0

情况:-

在我的代码中,我必须将 LWUIT 组件对象用于列表视图控件。控件是动态的,因此可以是任意数量。现在,我正在根据控件(以数字表示)创建组件对象,即,对于要首先创建的每个控件,组件对象正在创建。当控件增加时,此过程会减慢列表视图的呈现速度。

解决方案:-

如果我创建 Component 对象并在所有控件的循环中使用它,它将获取对象的引用,因此显示具有相同数据的所有列表视图项(控件)。现在我能够想到克隆我的对象并使用它来创建控件的最后一个选项。

问题:-

但是我在 LWUIT 中找不到任何方法可以实现对象的复制。

LWUIT 中有哪些替代方案可以解决此问题?

PS-列表视图项目是相同类型的,但具有不同的数据。

4

1 回答 1

2

使用 List 组件和 Renderer 设计模式创建一个“橡皮图章”组件,您可以在其中轻松显示大量元素。请参阅 Codename One博客中对此的解释。

首先创建这些classes:

public class ListUtil {

    private Vector data = new Vector();
    private Content[] contents;

    public ListUtil(Vector vData)
    {
        data = vData;
        contents = new Content[vData.size()];
    }

    public List createList(Display display, CListCell renderer, ActionListener listener)
    {
        CList theList;
        for(int i = 0; i < data.size(); i++)
        {
            contents[i] = new Content(String.valueOf(data.elementAt(i)));
        }
        theList = new CList(display, contents, renderer, listener);
        return theList;
    }
}

public class Content
{
    private String  row;

    public Content(String row)
    {
        this.row = row;
    }

    public String getRow()
    {
        return (row);
    }
}

public class CListCell extends Container implements ListCellRenderer {

    private Label focus = new Label("");

    public CListCell()
    {
        super();
        // create and add the components here among the components which will display data
    }
    public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected)
    {
        Content entry = null;
        if (value instanceof Content)
            entry = (Content)value;
        componentDisplayingDataAddedIntoThisListCellRenderer.setText(entry.getRow());
        return this;
    }
    public Component getListFocusComponent(List arg0)
    {
        return focus;
    }
}

public class CList extends List {
    private Display disp;
    public CList(Display display, Object[] data, CListCell renderer, ActionListener actionListener)
    {
        super(data);
        setListCellRenderer(renderer);
        setIgnoreFocusComponentWhenUnfocused(true);
        addActionListener(actionListener);
        setScrollAnimationSpeed(getScrollAnimationSpeed()/4);
        disp = display;
    }
    public void pointerReleased(int x,int y)
    {
        if (isEnabled() && hasFocus())
            super.pointerReleased(x, y);
    }
    public void keyReleased(int keyCode)
    {
        if (isEnabled() && hasFocus())
        {
            if (disp.getGameAction(keyCode) == Display.GAME_FIRE)
                pointerReleased(getX(),getY());
            else
                super.keyReleased(keyCode);
        }
    }
}

要创建您的List并将其添加到Form

public class myForm extends Form implements ActionListener
{
    private Vector listSource = // your vector of data
    private CListCell renderer = new CListCell();
    private List theList = (new ListUtil(listSource)).createList(Display.getInstance(),renderer, this);
    ...
    public void actionPerformed(ActionEvent evt)
    {
       if (evt.getSource() == theList)
            doSomething();
    }
}
于 2012-04-05T05:30:23.990 回答