public class DefaultListModel<T>

  1. Object
  2. DefaultListModel

ImplementsListModel<T>, MultipleSelectionListModel<T>

Known subtypesContactsModel

Default implementation of the list model based on a List of elements. The list model is an observable set of objects that com.codename1.ui.List uses to pull the data to display.

public void showForm() {
  Form hi = new Form("MultiList", new BorderLayout());

  int mm = Display.getInstance().convertToPixels(3);
  EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(mm * 3, mm * 4, 0), false);
  Image icon1 = URLImage.createToStorage(placeholder, "icon1", "http://www.georgerrmartin.com/wp-content/uploads/2013/03/GOTMTI2.jpg");
  Image icon2 = URLImage.createToStorage(placeholder, "icon2", "http://www.georgerrmartin.com/wp-content/uploads/2012/08/clashofkings.jpg");
  Image icon3 = URLImage.createToStorage(placeholder, "icon3", "http://www.georgerrmartin.com/wp-content/uploads/2013/03/stormswordsMTI.jpg");
  Image icon4 = URLImage.createToStorage(placeholder, "icon4", "http://www.georgerrmartin.com/wp-content/uploads/2012/08/feastforcrows.jpg");
  Image icon5 = URLImage.createToStorage(placeholder, "icon5", "http://georgerrmartin.com/gallery/art/dragons05.jpg");

  ArrayList> data = new ArrayList<>();
  data.add(createListEntry("A Game of Thrones", "1996", icon1));
  data.add(createListEntry("A Clash Of Kings", "1998", icon2));
  data.add(createListEntry("A Storm Of Swords", "2000", icon3));
  data.add(createListEntry("A Feast For Crows", "2005", icon4));
  data.add(createListEntry("A Dance With Dragons", "2011", icon5));
  data.add(createListEntry("The Winds of Winter", "2016 (please, please, please)", placeholder));
  data.add(createListEntry("A Dream of Spring", "Ugh", placeholder));

  DefaultListModel> model = new DefaultListModel<>(data);
  MultiList ml = new MultiList(model);
  hi.add(BorderLayout.CENTER, ml);
  hi.show();
}

private Map createListEntry(String name, String date, Image icon) {
  Map entry = new HashMap<>();
  entry.put("Line1", name);
  entry.put("Line2", date);
  entry.put("icon", icon);
  return entry;
}

Constructors

public DefaultListModel()Creates a new instance of DefaultListModel
public DefaultListModel(Vector<T> items)Creates a new instance of DefaultListModel
public DefaultListModel(Collection<T> items)Creates a new instance of DefaultListModel
public DefaultListModel(T... items)Creates a new instance of DefaultListModel

Methods

public T getItemAt(int index)Returns the item at the given offset
public int getSize()Returns the number of items in the list
public int getSelectedIndex()Returns the selected list offset
public void setSelectedIndex(int index)Sets the selected list offset can be set to -1 to clear selection
public void addItem(T item)Adds the specified item to the end of this list.
public void setItem(int index, T item)Change the item at the given index
public void addItemAtIndex(T item, int index)Adding an item to list at given index
public void removeItem(int index)Removes the item at the specified position in this list.
public void removeAll()Removes all elements from the model
public void addDataChangedListener(DataChangedListener l)Invoked to indicate interest in future change events
public void removeDataChangedListener(DataChangedListener l)Invoked to indicate no further interest in future change events
protected void fireDataChangedEvent(int status, int index)Broadcast a change event to all listeners
public void addSelectionListener(SelectionListener l)Invoked to indicate interest in future selection events
public void removeSelectionListener(SelectionListener l)Invoked to indicate no further interest in future selection events
public List<T> getList()Returns the internal list of items which makes traversal using iterators easier.
public void addSelectedIndices(int... indices)Adds indices to set of selected indices.
public void removeSelectedIndices(int... indices)Removes indices from the set of selected indices.
public int[] getSelectedIndices()Gets the selected indices in this model.
public void setSelectedIndices(int... indices)For use with multi-selection mode.
public boolean isMultiSelectionMode()Checks to see if this list model is in multi-selection mode.
public void setMultiSelectionMode(boolean multiSelectionMode)Enables or disables multi-selection mode.

Inherited methods

Constructor details

DefaultListModel

public DefaultListModel()
Creates a new instance of DefaultListModel

DefaultListModel

public DefaultListModel(Vector<T> items)
Creates a new instance of DefaultListModel

Parameters

items Vector<T>
the items in the model

DefaultListModel

public DefaultListModel(Collection<T> items)
Creates a new instance of DefaultListModel

Parameters

items Collection<T>
the items in the model

DefaultListModel

public DefaultListModel(T... items)
Creates a new instance of DefaultListModel

Parameters

items T...
the items in the model

Method details

getItemAt

public 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 int getSize()
Returns the number of items in the list

Returns

the number of items in the list

getSelectedIndex

public int getSelectedIndex()
Returns the selected list offset

Returns

the selected list index

setSelectedIndex

public 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

addItem

public 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

setItem

public void setItem(int index, T item)
Change the item at the given index

Parameters

index int
the offset for the item
item T
the value to set

addItemAtIndex

public void addItemAtIndex(T item, int index)
Adding an item to list at given index

Parameters

item T
  • the item to add
index int
  • the index position in the list

removeItem

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

Parameters

index int
the index of the item to removed

removeAll

public void removeAll()
Removes all elements from the model

addDataChangedListener

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

Parameters

l DataChangedListener
a data changed listener

removeDataChangedListener

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

Parameters

l DataChangedListener
a data changed listener

fireDataChangedEvent

protected void fireDataChangedEvent(int status, int index)
Broadcast a change event to all listeners

Parameters

status int
the status of the event
index int
the index changed

addSelectionListener

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

Parameters

l SelectionListener
a selection listener

removeSelectionListener

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

Parameters

l SelectionListener
a selection listener

getList

public List<T> getList()
Returns the internal list of items which makes traversal using iterators easier.

Returns

the list, notice that you shouldn’t modify it

addSelectedIndices

public void addSelectedIndices(int... indices)
Adds indices to set of selected indices.

Parameters

indices int...
Indices to add to selected indices.

removeSelectedIndices

public void removeSelectedIndices(int... indices)
Removes indices from the set of selected indices.

Parameters

indices int...
Indices to remove from selected indices.

getSelectedIndices

public int[] getSelectedIndices()
Gets the selected indices in this model. Indices should be returned in increasing order with no duplicates.

Returns

Selected indices in increasing order with no duplicates. If there are no selected indices, then this will return a zero-length array.

setSelectedIndices

public void setSelectedIndices(int... indices)

For use with multi-selection mode. Sets the selected indices in this model.

Note: This may fire multiple selectionChange events. For each “deselected” index, it will fire an event with the (oldIndex, newIndex) being (index, -1) (i.e. selected index changes from the index to -1. And for each newly selected index, it will fire the event with (oldIndex, newIndex) being (-1, index).

Parameters

indices int...
The indices to select.

Throws

IllegalArgumentException
If #isMultiSelectionMode() is false, and indices length is greater than 1.

isMultiSelectionMode

public boolean isMultiSelectionMode()
Checks to see if this list model is in multi-selection mode.

Returns

the multiSelectionMode

setMultiSelectionMode

public void setMultiSelectionMode(boolean multiSelectionMode)
Enables or disables multi-selection mode.

Parameters

multiSelectionMode boolean
the multiSelectionMode to set