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

霸气de小男生 提交于 2019-12-06 07:44:17

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