ArrayAdapter
open class ArrayAdapter<T : Any!> : BaseAdapter, Filterable, ThemedSpinnerAdapter
kotlin.Any | ||
↳ | android.widget.BaseAdapter | |
↳ | android.widget.ArrayAdapter |
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,android.view.View,android.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 androidx.recyclerview.widget.RecyclerView instead. RecyclerView offers similar features with better performance and more flexibility than ListView provides. See the Recycler View guide.
Summary
Public constructors | |
---|---|
ArrayAdapter(context: Context, resource: Int) Constructor |
|
ArrayAdapter(context: Context, resource: Int, textViewResourceId: Int) Constructor |
|
ArrayAdapter(context: Context, resource: Int, objects: Array<T>) Constructor. |
|
ArrayAdapter(context: Context, resource: Int, textViewResourceId: Int, objects: Array<T>) Constructor. |
|
ArrayAdapter(context: Context, resource: Int, objects: MutableList<T>) Constructor |
|
ArrayAdapter(context: Context, resource: Int, textViewResourceId: Int, objects: MutableList<T>) Constructor |
Public methods | |
---|---|
open Unit |
add(object: T?) Adds the specified object at the end of the array. |
open Unit |
addAll(collection: MutableCollection<out T>) Adds the specified Collection at the end of the array. |
open Unit |
addAll(vararg items: T) Adds the specified items at the end of the array. |
open Unit |
clear() Remove all elements from the list. |
open static ArrayAdapter<CharSequence!> |
createFromResource(context: Context, textArrayResId: Int, textViewResId: Int) Creates a new ArrayAdapter from external resources. |
open Array<CharSequence!>? |
Gets a string representation of the adapter data that can help |
open Context |
Returns the context associated with this array adapter. |
open Int |
getCount() |
open View! |
getDropDownView(position: Int, convertView: View?, parent: ViewGroup) Gets a |
open Resources.Theme? |
Returns the value previously set by a call to |
open Filter |
Returns a filter that can be used to constrain data with a filtering pattern. |
open T? |
Get the data item associated with the specified position in the data set. |
open Long | |
open Int |
getPosition(item: T?) Returns the position of the specified item in the array. |
open View |
Get a View that displays the data at the specified position in the data set. |
open Unit |
Inserts the specified object at the specified index in the array. |
open Unit | |
open Unit |
remove(object: T?) Removes the specified object from the array. |
open Unit |
setDropDownViewResource(resource: Int) Sets the layout resource to create the drop down views. |
open Unit |
setDropDownViewTheme(theme: Resources.Theme?) Sets the |
open Unit |
setNotifyOnChange(notifyOnChange: Boolean) Control whether methods that change the list ( |
open Unit |
sort(comparator: Comparator<in T>) Sorts the content of this adapter using the specified comparator. |
Inherited functions | |
---|---|
Public constructors
ArrayAdapter
ArrayAdapter(
context: Context,
resource: Int)
Constructor
Parameters | |
---|---|
context |
Context: The current context. This value cannot be null . |
resource |
Int: The resource ID for a layout file containing a TextView to use when instantiating views. |
ArrayAdapter
ArrayAdapter(
context: Context,
resource: Int,
textViewResourceId: Int)
Constructor
Parameters | |
---|---|
context |
Context: The current context. This value cannot 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
ArrayAdapter(
context: Context,
resource: Int,
objects: Array<T>)
Constructor. This constructor will result in the underlying data collection being immutable, so methods such as clear()
will throw an exception.
Parameters | |
---|---|
context |
Context: The current context. This value cannot be null . |
resource |
Int: The resource ID for a layout file containing a TextView to use when instantiating views. |
objects |
Array<T>: The objects to represent in the ListView. This value cannot be null . |
ArrayAdapter
ArrayAdapter(
context: Context,
resource: Int,
textViewResourceId: Int,
objects: Array<T>)
Constructor. This constructor will result in the underlying data collection being immutable, so methods such as clear()
will throw an exception.
Parameters | |
---|---|
context |
Context: The current context. This value cannot 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 |
Array<T>: The objects to represent in the ListView. This value cannot be null . |
ArrayAdapter
ArrayAdapter(
context: Context,
resource: Int,
objects: MutableList<T>)
Constructor
Parameters | |
---|---|
context |
Context: The current context. This value cannot be null . |
resource |
Int: The resource ID for a layout file containing a TextView to use when instantiating views. |
objects |
MutableList<T>: The objects to represent in the ListView. This value cannot be null . |
ArrayAdapter
ArrayAdapter(
context: Context,
resource: Int,
textViewResourceId: Int,
objects: MutableList<T>)
Constructor
Parameters | |
---|---|
context |
Context: The current context. This value cannot 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 |
MutableList<T>: The objects to represent in the ListView. This value cannot be null . |
Public methods
add
open fun add(object: T?): Unit
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 . |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if the underlying data collection is immutable |
addAll
open fun addAll(collection: MutableCollection<out T>): Unit
Adds the specified Collection at the end of the array.
Parameters | |
---|---|
collection |
MutableCollection<out T>: The Collection to add at the end of the array. This value cannot be null . |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if the addAll operation is not supported by this list |
java.lang.ClassCastException |
if the class of an element of the specified collection prevents it from being added to this list |
java.lang.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 |
java.lang.IllegalArgumentException |
if some property of an element of the specified collection prevents it from being added to this list |
addAll
open fun addAll(vararg items: T): Unit
Adds the specified items at the end of the array.
Parameters | |
---|---|
items |
T: The items to add at the end of the array. |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if the underlying data collection is immutable |
clear
open fun clear(): Unit
Remove all elements from the list.
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if the underlying data collection is immutable |
createFromResource
open static fun createFromResource(
context: Context,
textArrayResId: Int,
textViewResId: Int
): ArrayAdapter<CharSequence!>
Creates a new ArrayAdapter from external resources. The content of the array is obtained through android.content.res.Resources#getTextArray(int)
.
Parameters | |
---|---|
context |
Context: The application's environment. This value cannot 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. |
Return | |
---|---|
ArrayAdapter<CharSequence!> |
An ArrayAdapter. This value cannot be null . |
getAutofillOptions
open fun getAutofillOptions(): Array<CharSequence!>?
Gets a string representation of the adapter data that can help android.service.autofill.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 android.view.ViewStructure#setDataIsSensitive(boolean)
for more info.
Return | |
---|---|
Array<CharSequence!>? |
values from the string array used by createFromResource(android.content.Context,int,int) , or null if object was created otherwsie or if contents were dynamically changed after creation. |
getContext
open fun getContext(): Context
Returns the context associated with this array adapter. The context is used to create views from the resource passed to the constructor.
Return | |
---|---|
Context |
The Context associated with this adapter. This value cannot be null . |
getDropDownView
open fun getDropDownView(
position: Int,
convertView: View?,
parent: ViewGroup
): View!
Gets a android.view.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?: This value may be null . |
parent |
ViewGroup: This value cannot be null . |
Return | |
---|---|
View! |
a android.view.View corresponding to the data at the specified position. |
getDropDownViewTheme
open fun getDropDownViewTheme(): Resources.Theme?
Returns the value previously set by a call to setDropDownViewTheme(android.content.res.Resources.Theme)
.
Return | |
---|---|
Resources.Theme? |
This value may be null . |
getFilter
open fun getFilter(): Filter
Returns a filter that can be used to constrain data with a filtering pattern.
This method is usually implemented by android.widget.Adapter
classes.
Return | |
---|---|
Filter |
This value cannot be null . |
getItem
open fun getItem(position: Int): T?
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. |
Return | |
---|---|
T? |
This value may be null . |
getItemId
open fun getItemId(position: Int): Long
Parameters | |
---|---|
position |
Int: The position of the item within the adapter's data set whose row id we want. |
Return | |
---|---|
Long |
The id of the item at the specified position. |
getPosition
open fun getPosition(item: T?): Int
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 . |
Return | |
---|---|
Int |
The position of the specified item. |
getView
open fun getView(
position: Int,
convertView: View?,
parent: ViewGroup
): View
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 android.view.LayoutInflater#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?: This value may be null . |
parent |
ViewGroup: This value cannot be null . |
Return | |
---|---|
View |
This value cannot be null . |
insert
open fun insert(
object: T?,
index: Int
): Unit
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. |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if the underlying data collection is immutable |
remove
open fun remove(object: T?): Unit
Removes the specified object from the array.
Parameters | |
---|---|
object |
T?: The object to remove. This value may be null . |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
if the underlying data collection is immutable |
setDropDownViewResource
open fun setDropDownViewResource(resource: Int): Unit
Sets the layout resource to create the drop down views.
Parameters | |
---|---|
resource |
Int: the layout resource defining the drop down views |
setDropDownViewTheme
open fun setDropDownViewTheme(theme: Resources.Theme?): Unit
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 |
setNotifyOnChange
open fun setNotifyOnChange(notifyOnChange: Boolean): Unit
Control whether methods that change the list (add
, addAll(java.util.Collection)
, addAll(java.lang.Object[])
, insert
, remove
, clear
, sort(java.util.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
open fun sort(comparator: Comparator<in T>): Unit
Sorts the content of this adapter using the specified comparator.
Parameters | |
---|---|
comparator |
Comparator<in T>: The comparator used to sort the objects contained in this adapter. This value cannot be null . |