0

Situation:-

In my code I have to use the LWUIT Component object for the listview controls. The controls are dynamic and hence can be in any number. Right now I am creating Component objects according to the controls(in numbers) i.e.- for every control to be created first the Component object is creating. This process slows down the rendering of the listview when the controls are increasing.

Solution:-

If I create the Component object and use it in a loop for all the controls it is taking the reference of the object and hence displays all the listview items(controls) with the same data. Now I am able to think of one last option of Cloning my object and using it to create the controls.

Problem:-

But I can't find any way in LWUIT by which I can achieve the copying of object.

What can be the alternatives in LWUIT to solve this problem?

P.S.-The listview items are of same type, but with different data.

Shail Adi
  • 1,502
  • 4
  • 14
  • 35
  • Are the list items the same ? That is : are they of the same look ? And are they only different in the values they display ? – pheromix Apr 04 '12 at 13:41
  • Yes, the listview is meant to contain same kind of items. And the data in each item is different. – Shail Adi Apr 05 '12 at 04:39

1 Answers1

2

Use a List component and the Renderer design pattern to create a "rubber stamp" component where you can display a large number of elements easily. See an explanation of this in the Codename One blog.

Create these classes first :

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

To create your List and add it to a 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();
    }
}
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
pheromix
  • 18,213
  • 29
  • 88
  • 158
  • 1
    Good answer. I edited it to point at a more detailed explanation of what the list model/renderer are and how they can be used to scale massively. – Shai Almog Apr 05 '12 at 10:50
  • thank you very much : in fact what is `codenameone` ? It is the first time I hear it ! – pheromix Apr 05 '12 at 11:19
  • Its our new project, think of it as the natural progression of LWUIT. http://www.codenameone.com/ – Shai Almog Apr 05 '12 at 13:31