Override Population of Multi List

65 views
Skip to first unread message

Dan

unread,
Nov 4, 2013, 1:51:02 PM11/4/13
to codenameone...@googlegroups.com
Essentially, I would like to pass an array of custom objects (or Vector?) to a multi list so that:

1. when an item is clicked, I'm able to retrieve the custom object and get its properties
2. override how to populate the item's labels

Coming from Android, I would:

1. create my own BaseAdapter
2. override the adapter's public View getView(int position, View convertView, ViewGroup parent) {} method
3. instantiate my ListView in this fashion - listView.setAdapter(new CustomAdapter(getApplicationContext(), arrayOfCustomObjects));

Should I create a class extending and pass that to the multi list?

Shai Almog

unread,
Nov 5, 2013, 12:39:47 AM11/5/13
to codenameone...@googlegroups.com
You should use a List not a MultiList for this since MultiList is a simplification of List that already includes a builtin renderer.

The equivalent of Androids Adapter is the Renderer and ListModel. You can see a How Do I? video on list renderering which should help (although its designed for the GUI builder more than for handcoding), the developer guide goes into more detail on handcoding this.
The renderer is essentially a rubber stamp that displays a specific list element in a very similar way to the ListView on Android.

Personally, rather than deal with this I would rather have a list of maps or hashtables and not build my own renderer unless I REALLY need to. Just like in Android this is a very delicate and error prone process.

Dan

unread,
Nov 5, 2013, 12:50:42 PM11/5/13
to codenameone...@googlegroups.com
I forgot to mention that the list was clickable. When an item was clicked, it would return the appropriate customObject in order to retrieve one of its property and do something with it (ie. specific ID to load another Activity for instance).

Shai Almog

unread,
Nov 5, 2013, 2:49:48 PM11/5/13
to codenameone...@googlegroups.com
Do you mean an entry within the list or the element touched within the specific list entry?

The former is pretty easy, List.getSelectedIndex/Item() should return the touched item
The latter is a bit harder. If you use the GUI builder approach you can always use extractLastClickedComponent() from GenericListCellRenderer which will return the button clicked by the user (in your lists ActionListener).

If you don't go with that route you will need to add action listeners to the elements but just store the state of which element was touched. Then in the list action event you will have both the list selection entry and the specific element that was touched.
The logic here is that the renderer is stateless (rubber stamp) thus very high performance. When the user touches a button within the renderer it isn't necessarily associated with a list entry yet.

I plan to write a tutorial on this since its been a couple of years since I last documented this behavior but I'm not sure if I'll get to it this week.
Reply all
Reply to author
Forward
0 new messages