Skip to content

Most visited

Recently visited

navigation

ArrayAdapter

public class ArrayAdapter
extends BaseAdapter implements Filterable, ThemedSpinnerAdapter

java.lang.Object
   ↳ android.widget.BaseAdapter
     ↳ android.widget.ArrayAdapter<T>


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 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 a code example, see the CustomChoiceList sample.

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 RecyclerView instead. RecyclerView offers similar features with better performance and more flexibility than ListView provides. See the Recycler View guide.

Summary

Inherited constants

From interface android.widget.Adapter

Public constructors

ArrayAdapter(Context context, int resource)

Constructor

ArrayAdapter(Context context, int resource, int textViewResourceId)

Constructor

ArrayAdapter(Context context, int resource, 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, int textViewResourceId, List<T> objects)

Constructor

Public methods

void add(T object)

Adds the specified object at the end of the array.

void addAll(T... items)

Adds the specified items at the end of the array.

void addAll(Collection<? extends T> collection)

Adds the specified Collection 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.

CharSequence[] getAutofillOptions()

Gets a string representation of the adapter data that can help AutofillService autofill the view backed by the adapter.

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.

Inherited methods

From class android.widget.BaseAdapter
From class java.lang.Object
From interface android.widget.ListAdapter
From interface android.widget.SpinnerAdapter
From interface android.widget.Filterable
From interface android.widget.ThemedSpinnerAdapter
From interface android.widget.Adapter

Public constructors

ArrayAdapter

added in API level 1
ArrayAdapter (Context context, 
                int resource)

Constructor

Parameters
context Context: The current context.

This value must never be null.

resource int: The resource ID for a layout file containing a TextView to use when instantiating views.

ArrayAdapter

added in API level 1
ArrayAdapter (Context context, 
                int resource, 
                int textViewResourceId)

Constructor

Parameters
context Context: The current context.

This value must never be null.

resource int: The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId int: The id of the TextView within the layout resource to be populated

ArrayAdapter

added in API level 1
ArrayAdapter (Context context, 
                int resource, 
                T[] objects)

Constructor

Parameters
context Context: The current context.

This value must never be null.

resource int: The resource ID for a layout file containing a TextView to use when instantiating views.

objects T: The objects to represent in the ListView.

This value must never be null.

ArrayAdapter

added in API level 1
ArrayAdapter (Context context, 
                int resource, 
                int textViewResourceId, 
                T[] objects)

Constructor

Parameters
context Context: The current context.

This value must never be null.

resource int: The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId int: The id of the TextView within the layout resource to be populated

objects T: The objects to represent in the ListView.

This value must never be null.

ArrayAdapter

added in API level 1
ArrayAdapter (Context context, 
                int resource, 
                List<T> objects)

Constructor

Parameters
context Context: The current context.

This value must never be null.

resource int: The resource ID for a layout file containing a TextView to use when instantiating views.

objects List: The objects to represent in the ListView.

This value must never be null.

ArrayAdapter

added in API level 1
ArrayAdapter (Context context, 
                int resource, 
                int textViewResourceId, 
                List<T> objects)

Constructor

Parameters
context Context: The current context.

This value must never be null.

resource int: The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId int: The id of the TextView within the layout resource to be populated

objects List: The objects to represent in the ListView.

This value must never be null.

Public methods

add

added in API level 1
void add (T object)

Adds the specified object at the end of the array.

Parameters
object T: The object to add at the end of the array.

This value may be null.

addAll

added in API level 11
void addAll (T... items)

Adds the specified items at the end of the array.

Parameters
items T: The items to add at the end of the array.

addAll

added in API level 11
void addAll (Collection<? extends T> collection)

Adds the specified Collection at the end of the array.

Parameters
collection Collection: The Collection to add at the end of the array.

This value must never be null.

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

clear

added in API level 1
void clear ()

Remove all elements from the list.

createFromResource

added in API level 1
ArrayAdapter<CharSequence> createFromResource (Context context, 
                int textArrayResId, 
                int textViewResId)

Creates a new ArrayAdapter from external resources. The content of the array is obtained through getTextArray(int).

Parameters
context Context: The application's environment.

This value must never be null.

textArrayResId int: The identifier of the array to use as the data source.

textViewResId int: The identifier of the layout used to create views.

Returns
ArrayAdapter<CharSequence> An ArrayAdapter.

This value will never be null.

getAutofillOptions

added in API level 26
CharSequence[] getAutofillOptions ()

Gets a string representation of the adapter data that can help AutofillService autofill the view backed by the adapter.

It should only be set (i.e., non-null if the values do not represent PII (Personally Identifiable Information - sensitive data such as email addresses, credit card numbers, passwords, etc...). For example, it's ok to return a list of month names, but not a list of usernames. A good rule of thumb is that if the adapter data comes from static resources, such data is not PII - see setDataIsSensitive(boolean) for more info.

Returns
CharSequence[] values from the string array used by createFromResource(Context, int, int), or null if object was created otherwsie or if contents were dynamically changed after creation.

getContext

added in API level 1
Context getContext ()

Returns the context associated with this array adapter. The context is used to create views from the resource passed to the constructor.

Returns
Context The Context associated with this adapter.

This value will never be null.

getCount

added in API level 1
int getCount ()

How many items are in the data set represented by this Adapter.

Returns
int Count of items.

getDropDownView

added in API level 1
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.

Parameters
position int: index of the item whose view we want.

convertView View: 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 ViewGroup: the parent that this view will eventually be attached to

This value must never be null.

Returns
View a View corresponding to the data at the specified position.

getDropDownViewTheme

added in API level 23
Resources.Theme getDropDownViewTheme ()

Returns the value previously set by a call to setDropDownViewTheme(Theme).

Returns
Resources.Theme the Resources.Theme against which drop-down views are inflated, or null if one has not been explicitly set

This value may be null.

getFilter

added in API level 1
Filter getFilter ()

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

This method is usually implemented by Adapter classes.

Returns
Filter a filter used to constrain data

This value will never be null.

getItem

added in API level 1
T getItem (int position)

Get the data item associated with the specified position in the data set.

Parameters
position int: Position of the item whose data we want within the adapter's data set.

Returns
T The data at the specified position.

This value may be null.

getItemId

added in API level 1
long getItemId (int position)

Get the row id associated with the specified position in the list.

Parameters
position int: The position of the item within the adapter's data set whose row id we want.

Returns
long The id of the item at the specified position.

getPosition

added in API level 1
int getPosition (T item)

Returns the position of the specified item in the array.

Parameters
item T: The item to retrieve the position of.

This value may be null.

Returns
int The position of the specified item.

getView

added in API level 1
View getView (int position, 
                View convertView, 
                ViewGroup parent)

Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.

Parameters
position int: The position of the item within the adapter's data set of the item whose view we want.

convertView View: 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 getViewTypeCount() and getItemViewType(int)).

parent ViewGroup: The parent that this view will eventually be attached to

This value must never be null.

Returns
View A View corresponding to the data at the specified position.

This value will never be null.

insert

added in API level 1
void insert (T object, 
                int index)

Inserts the specified object at the specified index in the array.

Parameters
object T: The object to insert into the array.

This value may be null.

index int: The index at which the object must be inserted.

notifyDataSetChanged

added in API level 1
void notifyDataSetChanged ()

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

remove

added in API level 1
void remove (T object)

Removes the specified object from the array.

Parameters
object T: The object to remove.

This value may be null.

setDropDownViewResource

added in API level 1
void setDropDownViewResource (int resource)

Sets the layout resource to create the drop down views.

Parameters
resource int: the layout resource defining the drop down views

See also:

setDropDownViewTheme

added in API level 23
void setDropDownViewTheme (Resources.Theme theme)

Sets the 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.

Parameters
theme Resources.Theme: the theme against which to inflate drop-down views or null to use the theme from the adapter's context

See also:

setNotifyOnChange

added in API level 1
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 boolean: if true, modifications to the list will automatically call notifyDataSetChanged()

sort

added in API level 3
void sort (Comparator<? super T> comparator)

Sorts the content of this adapter using the specified comparator.

Parameters
comparator Comparator: The comparator used to sort the objects contained in this adapter.

This value must never be null.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)