[TIP] Creating dynamic List in Mobility Pack for NetBeans 5.0

It's easy to create a List in Visual Mobile Designer of Mobility Pack. You just drag and drop components - first add List component into your Visual MIDlet and then add all the ListItems to the List. Of course you'll have to rename the ListItems to some reasonable values. Asssign an action to every ListItem and then the List will work as you'd expect. You don't have to write a line of code.

When you want to create an list with dynamic content then it's little bit complicated(you'll have to code ;)) but still doable.


Create Dynamic List
This example will be as simple as possible. Let have a String array and construct the List from it. We will show only part of this array and we will dynamically show more/less items of this list. When you'll select an list element from the list then it will show it's name in a different form. This is IMO enought to show how to work with a dynamic list.

How to do it - the steps
Create new Mobility project and create a Visual MIDlet in it. Drag and drop List component from palette to Flow designer. Drag and drop TextBox component to the Flow Design. Rename instance of the List to dynamicList and 'TextBox' to showPressed. You can do it either by inline editing name the name of component in Flow design or by changing 'Instance Name' in Properties window. Then add three commands to the List. Name them Exit, Add, Remove. Point the Exit command to 'Exit Point' of 'Mobile Device'. Point from 'Start Point' to the list. Add Back command to the showPressed form. Open Action property of the Back command. Select 'Switch to Screen' and set 'Target screen' to the dynamicList.
Then go to Source code by pressing 'Source code' button on top-left of the Flow designer window.

Create new private field dynamicListListener in Source code of the visual midlet. Here is the code:
    private CommandListener dynamicListListener = new CommandListener() {
        public void commandAction(Command command, Displayable displayable) {
            if (displayable == get_dynamicList()) {
                if (command == exitCommand1) {
                    exitMIDlet();
                }
                if (command == dynamicList.SELECT_COMMAND) {
                    String __selectedString;
                    try {
                        __selectedString = get_dynamicList().getString(get_dynamicList().getSelectedIndex());
                    } catch (IndexOutOfBoundsException e) {
                        __selectedString = null;
                    }
                    if (__selectedString != null) {
                        get_showPressed().setString(__selectedString);
                        DynamicList.this.getDisplay().setCurrent(get_showPressed());
                    }
                }
            }
        }
    };
You can modify the behaviour of the listener as you need.
Next step is to add the listener to the list. Just add this code to post-init section code of the "public javax.microedition.lcdui.List get_dynamicList()" method:
     dynamicList.setCommandListener(dynamicListListener);
     createDynamicList(); 
Method createDynamicList() adds ListElements to the list. If your list should change every time when user displays the list you'll have to add this method even to pre-action code area of all transitions pointing to this List. I added 'Add' and 'Remove' command to the example project to show how to manage it.

Example of solution This example show how to do it in Mobility Pack 5.0.
Example project with the source code
There are two solutions of this problem. See comments in the Source code.
Comments:

Great writeup!!! Thanks!!

Posted by Spinlock on March 06, 2006 at 09:51 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This is the main channel to share news, tips and other stuff related to my work. Sometimes you can find even personal stuff. Enjoy your reading...

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
news
Blogroll

No bookmarks in folder

OldNews
MyTrash
NavBar
NetBeans
LinksToBeExpanded