public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSpinnerAdapter
However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.
To use something other than TextViews for the array display, for instance, ImageViews,
or to have some of data besides toString() results fill the views,
override getView(int, View, ViewGroup)
to return the type of view you want.
IGNORE_ITEM_VIEW_TYPE, NO_SELECTION
Constructor and Description |
---|
ArrayAdapter(Context context,
int resource)
Constructor
|
ArrayAdapter(Context context,
int resource,
int textViewResourceId)
Constructor
|
ArrayAdapter(Context context,
int resource,
int textViewResourceId,
List<T> objects)
Constructor
|
ArrayAdapter(Context context,
int resource,
int textViewResourceId,
T[] objects)
Constructor
|
ArrayAdapter(Context context,
int resource,
List<T> objects)
Constructor
|
ArrayAdapter(Context context,
int resource,
T[] objects)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
add(T object)
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 |
addAll(T... items)
Adds the specified items at the end of the array.
|
void |
clear()
Remove all elements from the list.
|
static ArrayAdapter<CharSequence> |
createFromResource(Context context,
int textArrayResId,
int textViewResId)
Creates a new ArrayAdapter from external resources.
|
Context |
getContext()
Returns the context associated with this array adapter.
|
int |
getCount()
How many items are in the data set represented by this Adapter.
|
View |
getDropDownView(int position,
View convertView,
ViewGroup parent)
Gets a
View that displays in the drop down popup
the data at the specified position in the data set. |
Resources.Theme |
getDropDownViewTheme()
Returns the value previously set by a call to
#setDropDownViewTheme(Theme) . |
Filter |
getFilter()
Returns a filter that can be used to constrain data with a filtering
pattern.
|
T |
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.
|
View |
getView(int position,
View convertView,
ViewGroup parent)
Get a View that displays the data at the specified position in the data set.
|
void |
insert(T object,
int index)
Inserts the specified object at the specified index in the array.
|
void |
notifyDataSetChanged()
Notifies the attached observers that the underlying data has been changed
and any View reflecting the data set should refresh itself.
|
void |
remove(T object)
Removes the specified object from the array.
|
void |
setDropDownViewResource(int resource)
Sets the layout resource to create the drop down views.
|
void |
setDropDownViewTheme(Resources.Theme theme)
Sets the
Resources.Theme against which drop-down views are
inflated. |
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() . |
void |
sort(Comparator<? super T> comparator)
Sorts the content of this adapter using the specified comparator.
|
areAllItemsEnabled, getItemViewType, getViewTypeCount, hasStableIds, isEmpty, isEnabled, notifyDataSetInvalidated, registerDataSetObserver, unregisterDataSetObserver
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getItemViewType, getViewTypeCount, hasStableIds, isEmpty, registerDataSetObserver, unregisterDataSetObserver
public ArrayAdapter(Context context, @LayoutRes int resource)
context
- The current context.resource
- The resource ID for a layout file containing a TextView to use when
instantiating views.public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId)
context
- The current context.resource
- The resource ID for a layout file containing a layout to use when
instantiating views.textViewResourceId
- The id of the TextView within the layout resource to be populatedpublic ArrayAdapter(Context context, @LayoutRes int resource, T[] objects)
context
- The current context.resource
- The resource ID for a layout file containing a TextView to use when
instantiating views.objects
- The objects to represent in the ListView.public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId, T[] objects)
context
- The current context.resource
- The resource ID for a layout file containing a layout to use when
instantiating views.textViewResourceId
- The id of the TextView within the layout resource to be populatedobjects
- The objects to represent in the ListView.public ArrayAdapter(Context context, @LayoutRes int resource, List<T> objects)
context
- The current context.resource
- The resource ID for a layout file containing a TextView to use when
instantiating views.objects
- The objects to represent in the ListView.public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId, List<T> objects)
context
- The current context.resource
- The resource ID for a layout file containing a layout to use when
instantiating views.textViewResourceId
- The id of the TextView within the layout resource to be populatedobjects
- The objects to represent in the ListView.public void add(T object)
object
- The object to add at the end of the array.public void addAll(Collection<? extends T> collection)
collection
- The Collection to add at the end of the array.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 listpublic void addAll(T... items)
items
- The items to add at the end of the array.public void insert(T object, int index)
object
- The object to insert into the array.index
- The index at which the object must be inserted.public void remove(T object)
object
- The object to remove.public void clear()
public void sort(Comparator<? super T> comparator)
comparator
- The comparator used to sort the objects contained
in this adapter.public void notifyDataSetChanged()
BaseAdapter
notifyDataSetChanged
in class BaseAdapter
public void setNotifyOnChange(boolean notifyOnChange)
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.notifyOnChange
- if true, modifications to the list will
automatically call notifyDataSetChanged()
public Context getContext()
public int getCount()
Adapter
public T getItem(int position)
Adapter
public int getPosition(T item)
item
- The item to retrieve the position of.public long getItemId(int position)
Adapter
public View getView(int position, View convertView, ViewGroup parent)
Adapter
LayoutInflater.inflate(int, android.view.ViewGroup, boolean)
to specify a root view and to prevent attachment to the root.getView
in interface Adapter
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 topublic void setDropDownViewResource(@LayoutRes int resource)
Sets the layout resource to create the drop down views.
resource
- the layout resource defining the drop down viewsgetDropDownView(int, android.view.View, android.view.ViewGroup)
public void setDropDownViewTheme(Resources.Theme theme)
Resources.Theme
against which drop-down views are
inflated.
By default, drop-down views are inflated against the theme of the
Context
passed to the adapter's constructor.
setDropDownViewTheme
in interface ThemedSpinnerAdapter
theme
- the theme against which to inflate drop-down views or
null
to use the theme from the adapter's contextgetDropDownView(int, View, ViewGroup)
public Resources.Theme getDropDownViewTheme()
ThemedSpinnerAdapter
#setDropDownViewTheme(Theme)
.getDropDownViewTheme
in interface ThemedSpinnerAdapter
Resources.Theme
against which drop-down views are
inflated, or null
if one has not been explicitly setpublic View getDropDownView(int position, View convertView, ViewGroup parent)
SpinnerAdapter
View
that displays in the drop down popup
the data at the specified position in the data set.getDropDownView
in interface SpinnerAdapter
getDropDownView
in class BaseAdapter
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 toView
corresponding to the data at the
specified position.public static ArrayAdapter<CharSequence> createFromResource(Context context, @ArrayRes int textArrayResId, @LayoutRes int textViewResId)
Resources.getTextArray(int)
.context
- The application's environment.textArrayResId
- The identifier of the array to use as the data source.textViewResId
- The identifier of the layout used to create views.public Filter getFilter()
Filterable
Returns a filter that can be used to constrain data with a filtering pattern.
This method is usually implemented by Adapter
classes.
getFilter
in interface Filterable