Class DefaultListModel<T>

java.lang.Object
com.codename1.ui.list.DefaultListModel<T>
All Implemented Interfaces:
ListModel<T>, MultipleSelectionListModel<T>
Direct Known Subclasses:
ContactsModel

public class DefaultListModel<T> extends Object implements MultipleSelectionListModel<T>

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;
}
  • 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: the items in the model
    • DefaultListModel

      public DefaultListModel(Collection<T> items)

      Creates a new instance of DefaultListModel

      Parameters
      • items: the items in the model
    • DefaultListModel

      public DefaultListModel(T... items)

      Creates a new instance of DefaultListModel

      Parameters
      • items: the items in the model
  • Method Details

    • getItemAt

      public T getItemAt(int index)

      Returns the item at the given offset

      Parameters
      • index: an index into this list
      Returns

      the item at the specified index

      Specified by:
      getItemAt in interface ListModel<T>
    • getSize

      public int getSize()

      Returns the number of items in the list

      Returns

      the number of items in the list

      Specified by:
      getSize in interface ListModel<T>
    • getSelectedIndex

      public int getSelectedIndex()

      Returns the selected list offset

      Returns

      the selected list index

      Specified by:
      getSelectedIndex in interface ListModel<T>
    • setSelectedIndex

      public void setSelectedIndex(int index)

      Sets the selected list offset can be set to -1 to clear selection

      Parameters
      • index: an index into this list
      Specified by:
      setSelectedIndex in interface ListModel<T>
    • 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: the item to be added
      Specified by:
      addItem in interface ListModel<T>
    • setItem

      public void setItem(int index, T item)

      Change the item at the given index

      Parameters
      • index: the offset for the item

      • item: the value to set

    • addItemAtIndex

      public void addItemAtIndex(T item, int index)

      Adding an item to list at given index

      Parameters
      • item: - the item to add

      • index: - the index position in the list

    • removeItem

      public void removeItem(int index)

      Removes the item at the specified position in this list.

      Parameters
      • index: the index of the item to removed
      Specified by:
      removeItem in interface ListModel<T>
    • 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: a data changed listener
      Specified by:
      addDataChangedListener in interface ListModel<T>
    • removeDataChangedListener

      public void removeDataChangedListener(DataChangedListener l)

      Invoked to indicate no further interest in future change events

      Parameters
      • l: a data changed listener
      Specified by:
      removeDataChangedListener in interface ListModel<T>
    • fireDataChangedEvent

      protected void fireDataChangedEvent(int status, int index)

      Broadcast a change event to all listeners

      Parameters
      • status: the status of the event

      • index: the index changed

    • addSelectionListener

      public void addSelectionListener(SelectionListener l)

      Invoked to indicate interest in future selection events

      Parameters
      • l: a selection listener
      Specified by:
      addSelectionListener in interface ListModel<T>
    • removeSelectionListener

      public void removeSelectionListener(SelectionListener l)

      Invoked to indicate no further interest in future selection events

      Parameters
      • l: a selection listener
      Specified by:
      removeSelectionListener in interface ListModel<T>
    • 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: Indices to add to selected indices.
      Specified by:
      addSelectedIndices in interface MultipleSelectionListModel<T>
    • removeSelectedIndices

      public void removeSelectedIndices(int... indices)

      Removes indices from the set of selected indices.

      Parameters
      • indices: Indices to remove from selected indices.
      Since

      6.0

      Specified by:
      removeSelectedIndices in interface MultipleSelectionListModel<T>
    • getSelectedIndices

      public int[] getSelectedIndices()

      Gets the selected indices in this model. Indices should be returned in increasing order with no duplicates.

      Returns
      Since

      6.0

      Specified by:
      getSelectedIndices in interface MultipleSelectionListModel<T>
      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: The indices to select.
      Throws
      • IllegalArgumentException: If #isMultiSelectionMode() is false, and indices length is greater than 1.
      Since

      6.0

      See also
      • #setMultiSelectionMode(boolean)

      • #isMultiSelectionMode()

      Specified by:
      setSelectedIndices in interface MultipleSelectionListModel<T>
    • isMultiSelectionMode

      public boolean isMultiSelectionMode()

      Checks to see if this list model is in multi-selection mode.

      Returns

      the multiSelectionMode

      Since

      6.0

    • setMultiSelectionMode

      public void setMultiSelectionMode(boolean multiSelectionMode)

      Enables or disables multi-selection mode.

      Parameters
      • multiSelectionMode: the multiSelectionMode to set
      Since

      6.0