BlackBerry - Add items to a ListField

陌路散爱 提交于 2019-11-28 22:01:57

You probably want to look at using an ObjectListField. Handling the select action is done throught the containing Screen object, I've done this below using a MenuItem, I'm not really sure how to set a default select listener, you may have to detect key and trackwheel events.

Some example code for you: (not tested!)

MainScreen screen = new MainScreen();
screen.setTitle("my test");

final ObjectListField list = new ObjectLIstField();
String[] items = new String[] { "Item 1", "Item 2", "Item 3" };
list.set(items);

screen.addMenuItem(new MenuItem("Select", 100, 1) {
    public void run() {
        int selectedIndex = list.getSelectedIndex();
        String item = (String)list.get(selectedIndex);
        // Do someting with item
    });
screen.add(list);

You can override the navigationClick method like this:

ObjectListField list = new ObjectListField()
{
    protected boolean navigationClick(int status, int time)
    {
        // Your implementation here.
    }
};
final class SimpleListScreen extends MainScreen
{
    public SimpleListScreen()
    {
        super(Manager.NO_VERTICAL_SCROLL);

        setTitle("Simple List Demo");

        add(new LabelField("My list", LabelField.FIELD_HCENTER));
        add(new SeparatorField());

        Manager mainManager = getMainManager();

        SimpleList listField = new SimpleList(mainManager);

        listField.add("Item 1");
        listField.add("Item 2");
        listField.add("Item 3");
        }
    }

    //add listener so that when an item is chosen,it will do something
private ListField fList = new ListField(){
        protected boolean navigationClick(int status, int time) {
            System.out.println("omt Click");
            return true;
        };
    };
Ian1971

You can detect the click on each list item by overriding

protected boolean navigationClick(int status,int time)

Then you just need to work out what to do in response to the click. The way I did this was by using an anonymous class, set for each list item.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!