2

How do you usually deal with Lists and the fact that they don't have a property to clearly identity an specific item ? So far, the only solucion I could come up with is to put the key I use at the beginning, followed by a hyphen and the text that is shown on every item. This way when I retrieve the text from the selected item I can get the key for the item.

This is how I do it, but surely there's gotta be a better solution and I'd really like that you could share your experience in this kind of scenarios.

Thanks in advance.

enter image description here

eddy
  • 4,373
  • 16
  • 60
  • 94

1 Answers1

2

The picture ooks like you keep all the data managed in your application inside the text of the items of a standard list.

Better hava a separate class for the data container objects and an overview screen derived from List that takes an array of those container objects and instantiate the Items from that. This screen could then provide a method

DataContainer getSelectedObject()

which uses getSelectedIndex() internally to look up the object.

More specifically (Overview.java)

package mvc.midlet;

import javax.microedition.lcdui.List;

public class Overview extends List {

    private final DomainObject[] data;
    public static Overview create(DomainObject[] data) {
        int i = 0;
        for(; i < data.length; i++) {
            if(data[i] == null) break;
        }
        String[] names = new String[i];
        for(int j = 0; j < i; j++) {
            names[j] = data[j].name;
        }
        return new Overview(names, data);
    }

    protected Overview(String names[], DomainObject[] data) {
        super("Overview", IMPLICIT, names, null);
        this.data = data;
    }

    public DomainObject getSelectedObject() {
        return data[this.getSelectedIndex()];
    }
}
Gregor Ophey
  • 817
  • 6
  • 12
  • Sorry to bother you Gregor, but do you think you could provide a basic example or explain a little more in detail your approach? Thank you – Axel Jun 14 '14 at 13:04
  • Thanks @Gregor. It's not the I want to be spoon-fed, but I'm not sure what you mean by "instantiate the Items from that". I thought Lists can only have string items. Is there any way to store object is List? Do you think you could explain that part?? – eddy Jun 16 '14 at 12:53
  • @eddy : this is an extension of the Overview from my answer to your [earlier question](http://stackoverflow.com/questions/24119067/how-do-you-organize-your-code-in-a-j2me-project/24150485#24150485) – Gregor Ophey Jun 17 '14 at 20:48