Class ArrayAdapter<T>
- All Implemented Interfaces:
Adapter
,Filterable
,ListAdapter
,SpinnerAdapter
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
-
Field Summary
Fields inherited from interface icyllis.modernui.widget.Adapter
IGNORE_ITEM_VIEW_TYPE, NO_SELECTION
-
Constructor Summary
ConstructorDescriptionArrayAdapter
(Context context, List<T> objects) ConstructorArrayAdapter
(Context context, T[] objects) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the specified object at the end of the array.void
addAll
(Collection<? extends T> collection) Adds the specified Collection at the end of the array.void
Adds the specified items at the end of the array.void
clear()
Remove all elements from the list.int
getCount()
How many items are in the data set represented by this Adapter.getDropDownView
(int position, View convertView, ViewGroup parent) Gets aView
that displays in the drop-down popup the data at the specified position in the data set.Returns a filter that can be used to constrain data with a filtering pattern.getItem
(int position) Get the data item associated with the specified position in the data set.long
getItemId
(int position) Get the row id associated with the specified position in the list.int
getPosition
(T item) Returns the position of the specified item in the array.Get a View that displays the data at the specified position in the data set.void
Inserts the specified object at the specified index in the array.void
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.void
Removes the specified object from the array.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 callnotifyDataSetChanged()
.void
sort
(Comparator<? super T> comparator) Sorts the content of this adapter using the specified comparator.Methods inherited from class icyllis.modernui.widget.BaseAdapter
areAllItemsEnabled, getItemViewType, getViewTypeCount, hasStableIds, isEmpty, isEnabled, notifyDataSetInvalidated, registerDataSetObserver, unregisterDataSetObserver
-
Constructor Details
-
ArrayAdapter
Constructor. This constructor will result in the underlying data collection being immutable, so methods such asclear()
will throw an exception.- Parameters:
objects
- The objects to represent in the ListView.
-
ArrayAdapter
Constructor- Parameters:
objects
- The objects to represent in the ListView.
-
-
Method Details
-
add
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
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 listClassCastException
- if the class of an element of the specified collection prevents it from being added to this listNullPointerException
- if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is nullIllegalArgumentException
- if some property of an element of the specified collection prevents it from being added to this list
-
addAll
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
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
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
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 classBaseAdapter
-
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 callnotifyDataSetChanged()
. 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 callnotifyDataSetChanged()
-
getCount
public int getCount()Description copied from interface:Adapter
How many items are in the data set represented by this Adapter. -
getItem
Description copied from interface:Adapter
Get the data item associated with the specified position in the data set. -
getPosition
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. -
getView
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 interfaceAdapter
- 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 (seeAdapter.getViewTypeCount()
andAdapter.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
Description copied from interface:SpinnerAdapter
Gets aView
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 interfaceSpinnerAdapter
- Overrides:
getDropDownView
in classBaseAdapter
- 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
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 interfaceFilterable
- Returns:
- a filter used to constrain data
-