Class ArrayAdapter<T>

java.lang.Object
icyllis.modernui.widget.BaseAdapter
icyllis.modernui.widget.ArrayAdapter<T>
All Implemented Interfaces:
Adapter, Filterable, ListAdapter, SpinnerAdapter

public class ArrayAdapter<T> extends BaseAdapter implements Filterable
You can use this adapter to provide views for an AdapterView, Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner.

By default, the array adapter creates a view by calling Object.toString() on each data object in the collection you provide, and places the result in a TextView. You may also customize what type of view is used for the data object in the collection. To customize what type of view is used for the data object, override getView(int, View, ViewGroup) and inflate a view resource.

For an example of using an array adapter with a ListView, see the Adapter Views guide.

For an example of using an array adapter with a Spinner, see the Spinners guide.

Note: If you are considering using array adapter with a ListView, consider using

invalid reference
RecyclerView
instead. RecyclerView offers similar features with better performance and more flexibility than ListView provides. See the Recycler View guide.

  • Constructor Details

    • ArrayAdapter

      public ArrayAdapter(Context context, @Nonnull T[] objects)
      Constructor. This constructor will result in the underlying data collection being immutable, so methods such as clear() will throw an exception.
      Parameters:
      objects - The objects to represent in the ListView.
    • ArrayAdapter

      public ArrayAdapter(Context context, @Nonnull List<T> objects)
      Constructor
      Parameters:
      objects - The objects to represent in the ListView.
  • Method Details

    • add

      public void add(@Nullable T object)
      Adds the specified object at the end of the array.
      Parameters:
      object - The object to add at the end of the array.
      Throws:
      UnsupportedOperationException - if the underlying data collection is immutable
    • addAll

      public void addAll(@Nonnull Collection<? extends T> collection)
      Adds the specified Collection at the end of the array.
      Parameters:
      collection - The Collection to add at the end of the array.
      Throws:
      UnsupportedOperationException - if the addAll operation is not supported by this list
      ClassCastException - if the class of an element of the specified collection prevents it from being added to this list
      NullPointerException - if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null
      IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to this list
    • addAll

      public void addAll(@Nonnull T[] items)
      Adds the specified items at the end of the array.
      Parameters:
      items - The items to add at the end of the array.
      Throws:
      UnsupportedOperationException - if the underlying data collection is immutable
    • insert

      public void insert(@Nullable T object, int index)
      Inserts the specified object at the specified index in the array.
      Parameters:
      object - The object to insert into the array.
      index - The index at which the object must be inserted.
      Throws:
      UnsupportedOperationException - if the underlying data collection is immutable
    • remove

      public void remove(@Nullable T object)
      Removes the specified object from the array.
      Parameters:
      object - The object to remove.
      Throws:
      UnsupportedOperationException - if the underlying data collection is immutable
    • clear

      public void clear()
      Remove all elements from the list.
      Throws:
      UnsupportedOperationException - if the underlying data collection is immutable
    • sort

      public void sort(@Nonnull Comparator<? super T> comparator)
      Sorts the content of this adapter using the specified comparator.
      Parameters:
      comparator - The comparator used to sort the objects contained in this adapter.
    • notifyDataSetChanged

      public void notifyDataSetChanged()
      Description copied from class: BaseAdapter
      Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
      Overrides:
      notifyDataSetChanged in class BaseAdapter
    • setNotifyOnChange

      public void setNotifyOnChange(boolean notifyOnChange)
      Control whether methods that change the list (add(T), addAll(Collection), addAll(Object[]), insert(T, int), remove(T), clear(), sort(Comparator)) automatically call notifyDataSetChanged(). If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view.

      The default is true, and calling notifyDataSetChanged() resets the flag to true.

      Parameters:
      notifyOnChange - if true, modifications to the list will automatically call notifyDataSetChanged()
    • getCount

      public int getCount()
      Description copied from interface: Adapter
      How many items are in the data set represented by this Adapter.
      Specified by:
      getCount in interface Adapter
      Returns:
      Count of items.
    • getItem

      public T getItem(int position)
      Description copied from interface: Adapter
      Get the data item associated with the specified position in the data set.
      Specified by:
      getItem in interface Adapter
      Parameters:
      position - Position of the item whose data we want within the adapter's data set.
      Returns:
      The data at the specified position, may be null.
    • getPosition

      public int getPosition(@Nullable T item)
      Returns the position of the specified item in the array.
      Parameters:
      item - The item to retrieve the position of.
      Returns:
      The position of the specified item.
    • getItemId

      public long getItemId(int position)
      Description copied from interface: Adapter
      Get the row id associated with the specified position in the list.
      Specified by:
      getItemId in interface Adapter
      Parameters:
      position - The position of the item within the adapter's data set whose row id we want.
      Returns:
      The id of the item at the specified position.
    • getView

      @Nonnull public View getView(int position, @Nullable View convertView, @Nonnull ViewGroup parent)
      Description copied from interface: Adapter
      Get a View that displays the data at the specified position in the data set. You can create a View manually.

      This method should not return null unless the item count is zero.

      Specified by:
      getView in interface Adapter
      Parameters:
      position - The position of the item within the adapter's data set of the item whose view we want.
      convertView - The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see Adapter.getViewTypeCount() and Adapter.getItemViewType(int)).
      parent - The parent that this view will eventually be attached to
      Returns:
      A View corresponding to the data at the specified position.
    • getDropDownView

      public View getDropDownView(int position, @Nullable View convertView, @Nonnull ViewGroup parent)
      Description copied from interface: SpinnerAdapter
      Gets a View that displays in the drop-down popup the data at the specified position in the data set.

      This method should not return null unless the item count is zero.

      Specified by:
      getDropDownView in interface SpinnerAdapter
      Overrides:
      getDropDownView in class BaseAdapter
      Parameters:
      position - index of the item whose view we want.
      convertView - the old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
      parent - the parent that this view will eventually be attached to
      Returns:
      a View corresponding to the data at the specified position.
    • getFilter

      @Nonnull public Filter getFilter()
      Description copied from interface: Filterable

      Returns a filter that can be used to constrain data with a filtering pattern.

      This method is usually implemented by Adapter classes.

      Specified by:
      getFilter in interface Filterable
      Returns:
      a filter used to constrain data