views:

8399

answers:

8

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!

+7  A: 

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);
roryf
A: 

Thanks Rory! It does work for adding the list items. I was able to display the three lines the way I wanted.

Now, like you said, I have the challenge of how to handle the select action. I'll investigate on how to detect key and trackwheel events. My idea is to open a new screen depending on which item was selected.

Any ideas on how to do this will be very much appreciated.

Thanks!

AbsolutG
A: 

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. Let me know if you need more detail.

Ian1971
A: 

I am interested in more detail.

Thanks.

+1  A: 

You can override the navigationClick method like this:

ObjectListField list = new ObjectListField()
{
    protected boolean navigationClick(int status, int time)
    {
     // Your implementation here.
    }
};
zechariahs
A: 

Hi, Can anyone tell me how to get all selected items from a list with checkboxes?

Please help

imMobile
A: 

Hey, I was wondering how could I add more than one string per line. i.e. I want to display a Name and details, similar format to how you can view contacts.

thanks!

Mike
A: 

Hey Mike, try to implement a paint function and use drawtext.

Netcyrix