j2me - Does a List have any property to keep track of 'key' that identify the item

社会主义新天地 提交于 2019-12-10 10:56:34

问题


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.


回答1:


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()];
    }
}


来源:https://stackoverflow.com/questions/24215603/j2me-does-a-list-have-any-property-to-keep-track-of-key-that-identify-the-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!