Most visited

Recently visited

Added in API level 1

ArrayAdapter

public class ArrayAdapter
extends BaseAdapter implements Filterable, ThemedSpinnerAdapter

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


具有任意对象数组支持的具体BaseAdapter。 默认情况下,这个类期望提供的资源id引用单个TextView。 如果您想使用更复杂的布局,请使用也需要字段ID的构造函数。 该字段ID应在较大的布局资源中引用TextView。

然而,TextView被引用,它将被填充数组中每个对象的toString()。 您可以添加自定义对象的列表或数组。 重写对象的toString()方法以确定列表中的项目将显示哪些文本。

要使用TextView以外的其他数据显示(例如ImageViews)或除了toString()结果之外还有一些数据填充视图,请覆盖 getView(int, View, ViewGroup)以返回所需的视图类型。

Summary

Inherited constants

From interface android.widget.Adapter

Public constructors

ArrayAdapter(Context context, int resource)

构造函数

ArrayAdapter(Context context, int resource, int textViewResourceId)

构造函数

ArrayAdapter(Context context, int resource, T[] objects)

构造函数

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

构造函数

ArrayAdapter(Context context, int resource, List<T> objects)

构造函数

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

构造函数

Public methods

void add(T object)

在数组的末尾添加指定的对象。

void addAll(T... items)

在数组末尾添加指定的项目。

void addAll(Collection<? extends T> collection)

在数组末尾添加指定的Collection。

void clear()

删除列表中的所有元素。

static ArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId)

从外部资源创建一个新的ArrayAdapter。

Context getContext()

返回与此阵列适配器关联的上下文。

int getCount()

本适配器所代表的数据集中有多少项目。

View getDropDownView(int position, View convertView, ViewGroup parent)

获取一个 View ,它在下拉式弹出 View中显示数据集中指定位置的数据。

Resources.Theme getDropDownViewTheme()

返回以前通过调用 setDropDownViewTheme(Theme)设置的值。

Filter getFilter()

返回可用于使用过滤模式约束数据的过滤器。

T getItem(int position)

获取与数据集中指定位置关联的数据项。

long getItemId(int position)

获取与列表中指定位置关联的行ID。

int getPosition(T item)

返回数组中指定项目的位置。

View getView(int position, View convertView, ViewGroup parent)

获取显示数据集中指定位置的数据的视图。

void insert(T object, int index)

将指定的对象插入数组中的指定索引处。

void notifyDataSetChanged()

通知附属的观察者,底层数据已被更改,任何反映数据集的视图都应该自行刷新。

void remove(T object)

从数组中删除指定的对象。

void setDropDownViewResource(int resource)

设置布局资源以创建下拉视图。

void setDropDownViewTheme(Resources.Theme theme)

设置 Resources.Theme针对哪个下拉视图被夸大。

void setNotifyOnChange(boolean notifyOnChange)

控制是否该改变列表的方法( add(T)insert(T, int)remove(T)clear() )自动调用 notifyDataSetChanged()

void sort(Comparator<? super T> 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)

构造函数

Parameters
context Context: The current context.
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)

构造函数

Parameters
context Context: The current context.
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)

构造函数

Parameters
context Context: The current context.
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.

ArrayAdapter

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

构造函数

Parameters
context Context: The current context.
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.

ArrayAdapter

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

构造函数

Parameters
context Context: The current context.
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.

ArrayAdapter

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

构造函数

Parameters
context Context: The current context.
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.

Public methods

add

Added in API level 1
void add (T object)

在数组的末尾添加指定的对象。

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

addAll

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

在数组末尾添加指定的项目。

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)

在数组末尾添加指定的Collection。

Parameters
collection 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

clear

Added in API level 1
void clear ()

删除列表中的所有元素。

createFromResource

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

从外部资源创建一个新的ArrayAdapter。 数组的内容通过getTextArray(int)获得。

Parameters
context Context: The application's environment.
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 .

getContext

Added in API level 1
Context getContext ()

返回与此阵列适配器关联的上下文。 上下文用于从传递给构造函数的资源创建视图。

Returns
Context The Context associated with this adapter.

getCount

Added in API level 1
int getCount ()

本适配器所代表的数据集中有多少项目。

Returns
int Count of items.

getDropDownView

Added in API level 1
View getDropDownView (int position, 
                View convertView, 
                ViewGroup parent)

获取一个 View ,它在下拉式弹出 View中显示数据集中指定位置的数据。

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
Returns
View a View corresponding to the data at the specified position.

getDropDownViewTheme

Added in API level 23
Resources.Theme getDropDownViewTheme ()

返回以前通过调用 setDropDownViewTheme(Theme)设置的值。

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

getFilter

Added in API level 1
Filter getFilter ()

返回可用于使用过滤模式约束数据的过滤器。

该方法通常由 Adapter类实现。

Returns
Filter a filter used to constrain data

getItem

Added in API level 1
T getItem (int position)

获取与数据集中指定位置关联的数据项。

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.

getItemId

Added in API level 1
long getItemId (int position)

获取与列表中指定位置关联的行ID。

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)

返回数组中指定项目的位置。

Parameters
item T: The item to retrieve the position of.
Returns
int The position of the specified item.

getView

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

获取显示数据集中指定位置的数据的视图。 您可以手动创建视图或从XML布局文件中对其进行膨胀。 当视图膨胀时,父视图(GridView,ListView ...)将应用默认布局参数,除非您使用inflate(int, android.view.ViewGroup, boolean)来指定根视图并防止附加到根。

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
Returns
View A View corresponding to the data at the specified position.

insert

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

将指定的对象插入数组中的指定索引处。

Parameters
object T: The object to insert into the array.
index int: The index at which the object must be inserted.

notifyDataSetChanged

Added in API level 1
void notifyDataSetChanged ()

通知附属的观察者,底层数据已被更改,任何反映数据集的视图都应该自行刷新。

remove

Added in API level 1
void remove (T object)

从数组中删除指定的对象。

Parameters
object T: The object to remove.

setDropDownViewResource

Added in API level 1
void setDropDownViewResource (int resource)

设置布局资源以创建下拉视图。

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

也可以看看:

setDropDownViewTheme

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

设置 Resources.Theme ,下拉视图被 Resources.Theme

默认情况下,下拉视图会根据传递给适配器构造函数的 Context的主题进行膨胀。

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

也可以看看:

setNotifyOnChange

Added in API level 1
void setNotifyOnChange (boolean notifyOnChange)

控制是否该改变列表的方法( add(T)insert(T, int)remove(T)clear() )自动调用notifyDataSetChanged() 如果设置为false,调用者必须手动调用notifyDataSetChanged()以使所做的更改反映在所附视图中。 默认值为true,并调用notifyDataSetChanged()将该标志重置为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)

使用指定的比较器对此适配器的内容进行排序。

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

Hooray!