BlackBerry - Add items to a ListField
Asked Answered
C

5

6

Can someone please give me a simple example on how to add three rows to a ListField so that the list shows something like this?

Item 1

Item 2

Item 3

I just want to show a list in which the user can select one of the items and the program would do something depending on the item selected.

I've search all over the internet but it seems impossible to find a simple example on how to do this (most examples I found are incomplete) and the blackberry documentation is terrible.

Thanks!

Cannery answered 26/12, 2008 at 16:32 Comment(0)
P
11

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);
Pterosaur answered 30/12, 2008 at 21:24 Comment(0)
C
2

You can override the navigationClick method like this:

ObjectListField list = new ObjectListField()
{
    protected boolean navigationClick(int status, int time)
    {
        // Your implementation here.
    }
};
Cypripedium answered 18/2, 2009 at 1:55 Comment(0)
C
1
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
Cubicle answered 3/4, 2012 at 4:16 Comment(0)
P
0

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.

Parasynthesis answered 10/2, 2009 at 12:47 Comment(0)
B
0
private ListField fList = new ListField(){
        protected boolean navigationClick(int status, int time) {
            System.out.println("omt Click");
            return true;
        };
    };
Bojorquez answered 29/2, 2012 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.