public interface ListModel<T>

Known subtypesContactsModel, DefaultListModel, FilterProxyListModel, MultipleSelectionListModel

Represents the data structure of the list, thus allowing a list to represent any potential data source by referencing different implementations of this interface, notice that we strongly discourage usage of lists.. E.g. a list model can be implemented in such a way that it retrieves data directly from storage (although caching would be recommended).

It is the responsibility of the list to notify observers (specifically the view com.codename1.ui.List of any changes to its state (items removed/added/changed etc.) thus the data would get updated on the view.

class GRMMModel implements ListModel> {
@Override
    public Map getItemAt(int index) {
        int idx = index % 7;
        switch(idx) {
            case 0:
                return createListEntry("A Game of Thrones " + index, "1996");
            case 1:
                return createListEntry("A Clash Of Kings " + index, "1998");
            case 2:
                return createListEntry("A Storm Of Swords " + index, "2000");
            case 3:
                return createListEntry("A Feast For Crows " + index, "2005");
            case 4:
                return createListEntry("A Dance With Dragons " + index, "2011");
            case 5:
                return createListEntry("The Winds of Winter " + index, "2016 (please, please, please)");
            default:
                return createListEntry("A Dream of Spring " + index, "Ugh");
        }
    }
@Override
    public int getSize() {
        return 1000000;
    }
@Override
    public int getSelectedIndex() {
        return 0;
    }
@Override
    public void setSelectedIndex(int index) {
    }
@Override
    public void addDataChangedListener(DataChangedListener l) {
    }
@Override
    public void removeDataChangedListener(DataChangedListener l) {
    }
@Override
    public void addSelectionListener(SelectionListener l) {
    }
@Override
    public void removeSelectionListener(SelectionListener l) {
    }
@Override
    public void addItem(Map item) {
    }
@Override
    public void removeItem(int index) {
    }
}

A ListModel can be used in conjunction with an com.codename1.components.ImageViewer to fetch images dynamically into the view:

Form hi = new Form("ImageViewer", new BorderLayout());
final EncodedImage placeholder = EncodedImage.createFromImage(
        FontImage.createMaterial(FontImage.MATERIAL_SYNC, s).
                scaled(300, 300), false);

class ImageList implements ListModel {
    private int selection;
    private String[] imageURLs = {
        "http://awoiaf.westeros.org/images/thumb/9/93/AGameOfThrones.jpg/300px-AGameOfThrones.jpg",
        "http://awoiaf.westeros.org/images/thumb/3/39/AClashOfKings.jpg/300px-AClashOfKings.jpg",
        "http://awoiaf.westeros.org/images/thumb/2/24/AStormOfSwords.jpg/300px-AStormOfSwords.jpg",
        "http://awoiaf.westeros.org/images/thumb/a/a3/AFeastForCrows.jpg/300px-AFeastForCrows.jpg",
        "http://awoiaf.westeros.org/images/7/79/ADanceWithDragons.jpg"
    };
    private Image[] images;
    private EventDispatcher listeners = new EventDispatcher();

    public ImageList() {
        this.images = new EncodedImage[imageURLs.length];
    }

    public Image getItemAt(final int index) {
        if(images[index] == null) {
            images[index] = placeholder;
            Util.downloadUrlToStorageInBackground(imageURLs[index], "list" + index, (e) -> {
                    try {
                        images[index] = EncodedImage.create(Storage.getInstance().createInputStream("list" + index));
                        listeners.fireDataChangeEvent(index, DataChangedListener.CHANGED);
                    } catch(IOException err) {
                        err.printStackTrace();
                    }
            });
        }
        return images[index];
    }

    public int getSize() {
        return imageURLs.length;
    }

    public int getSelectedIndex() {
        return selection;
    }

    public void setSelectedIndex(int index) {
        selection = index;
    }

    public void addDataChangedListener(DataChangedListener l) {
        listeners.addListener(l);
    }

    public void removeDataChangedListener(DataChangedListener l) {
        listeners.removeListener(l);
    }

    public void addSelectionListener(SelectionListener l) {
    }

    public void removeSelectionListener(SelectionListener l) {
    }

    public void addItem(Image item) {
    }

    public void removeItem(int index) {
    }
};

ImageList imodel = new ImageList();

ImageViewer iv = new ImageViewer(imodel.getItemAt(0));
iv.setImageList(imodel);
hi.add(BorderLayout.CENTER, iv);

Methods

public abstract T getItemAt(int index)Returns the item at the given offset
public abstract int getSize()Returns the number of items in the list
public abstract int getSelectedIndex()Returns the selected list offset
public abstract void setSelectedIndex(int index)Sets the selected list offset can be set to -1 to clear selection
public abstract void addDataChangedListener(DataChangedListener l)Invoked to indicate interest in future change events
public abstract void removeDataChangedListener(DataChangedListener l)Invoked to indicate no further interest in future change events
public abstract void addSelectionListener(SelectionListener l)Invoked to indicate interest in future selection events
public abstract void removeSelectionListener(SelectionListener l)Invoked to indicate no further interest in future selection events
public abstract void addItem(T item)Adds the specified item to the end of this list.
public abstract void removeItem(int index)Removes the item at the specified position in this list.

Method details

getItemAt

public abstract T getItemAt(int index)
Returns the item at the given offset

Parameters

index int
an index into this list

Returns

the item at the specified index

getSize

public abstract int getSize()
Returns the number of items in the list

Returns

the number of items in the list

getSelectedIndex

public abstract int getSelectedIndex()
Returns the selected list offset

Returns

the selected list index

setSelectedIndex

public abstract void setSelectedIndex(int index)
Sets the selected list offset can be set to -1 to clear selection

Parameters

index int
an index into this list

addDataChangedListener

public abstract void addDataChangedListener(DataChangedListener l)
Invoked to indicate interest in future change events

Parameters

l DataChangedListener
a data changed listener

removeDataChangedListener

public abstract void removeDataChangedListener(DataChangedListener l)
Invoked to indicate no further interest in future change events

Parameters

l DataChangedListener
a data changed listener

addSelectionListener

public abstract void addSelectionListener(SelectionListener l)
Invoked to indicate interest in future selection events

Parameters

l SelectionListener
a selection listener

removeSelectionListener

public abstract void removeSelectionListener(SelectionListener l)
Invoked to indicate no further interest in future selection events

Parameters

l SelectionListener
a selection listener

addItem

public abstract void addItem(T item)
Adds the specified item to the end of this list. An optional operation for mutable lists, it can throw an unsupported operation exception if a list model is not mutable.

Parameters

item T
the item to be added

removeItem

public abstract void removeItem(int index)
Removes the item at the specified position in this list.

Parameters

index int
the index of the item to removed