在BlackBerry上自定义列表字段项

时间:2010-11-27 20:15:23

标签: java blackberry

我有一个简单的列表字段类,当我选择特定的列表项时,它会显示默认菜单。我想自定义listfield项,以便在选择项目时将新屏幕推送到堆栈。我覆盖了trackwheeel()方法,但我无法使其工作。

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import java.util.Vector;



public class TeamListScreen extends UiApplication
{
     public static void main(String[] args)
     {
      TeamListScreen theApp = new TeamListScreen();
         theApp.enterEventDispatcher();
     }
     public TeamListScreen()
     {
         pushScreen(new ListFieldScreen());
     }
} 

class ListFieldScreen extends MainScreen
{
    private ListField _listField;
    private Vector _listElements;
    int listFieldIndex = -1;

    public ListFieldScreen() 
    {
        setTitle("List Field Sample");

_listElements = new Vector(); 
_listField = new ListField();
ListCallback _callback = new ListCallback();
_listField.setCallback(_callback);
_listField.setRowHeight(45);
//_listField.setChangeListener(this);
add(_listField);
initializeList();
_listField = new ListField(_listElements.size()) {

 protected void drawFocus(Graphics graphics, boolean on) {
 }

 protected boolean trackwheelClick(int status, int time) {

  listFieldIndex = _listField.getSelectedIndex();

  if (listFieldIndex < 0) {
   listFieldIndex = 0;

  }
  try {
   UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() {
     UiApplication.getUiApplication().pushScreen(new LiveScreen());
     // UiApplication.getUiApplication().popScreen(getActiveScreen());
    }
   });
  } catch (Exception e) {
  }
  return true;
 }

};


}

private void initializeList()
{
    String itemOne = "List item one";
    String itemTwo = "List item two";
    _listElements.addElement(itemOne);
    _listElements.addElement(itemTwo);
    reloadList();
}
private void reloadList()
{
    _listField.setSize(_listElements.size());
}

private class ListCallback implements ListFieldCallback 
{
    public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
    { 
       String text = (String)_listElements.elementAt(index); 
        g.drawText(text, 0, y, 0, w); 
    } 
    public Object get(ListField list, int index) 
    {
        return _listElements.elementAt(index); 
    } 
    public int indexOfList(ListField list, String prefix, int string) 
    { 
        return _listElements.indexOf(prefix, string); 
    } 
    public int getPreferredWidth(ListField list) 
    { 
        return Display.getWidth(); 
    } 
}
}

1 个答案:

答案 0 :(得分:0)

不推荐使用trackwheelClick()函数,您应该使用navigationClick()代替。

顺便说一句,您不需要使用UiApplication.getUiApplication()。invokeLater,因为它已经在事件队列中运行。