Android ArrayAdapter

January 8, 2018 Oclemy Android Adapter 2 minutes, 31 seconds

ArrayAdapter is concrete class that derives from the BaseAdapter and is backed by an array of arbitrary objects.

ArrayAdapter requires us to pass a single TextView as a resource as the layout. This is for simple layouts where you don't have to create a custom adapter.

For complex layouts you can create your own adapter that derives from this class.

ArrayAdapter can take as the data source as Lists, String Arrays or Arrays of custom objects.

If you use an Array of custom objects then be sure to override the toString() method so as to customize the string that will get displayed in the adapterview.

ArrayAdapter Definition

ArrayAdapter resides in the android.widget package:

package android.widget;

It derives from BaseAdapter:

public class ArrayAdapter extends BaseAdapter..{}

And implements Filterable and ThemedSpinnerAdapter interfaces:

public class ArrayAdapter extends BaseAdapter implements Filterable ThemedSpinnerAdapter{}

Creating ArrayAdapter Instance

ArrayAdapter provides several constructors for its creation. All these constructors must take at least a Context object and resource integer.

No. Constructor
1. ArrayAdapter(Context context, int resource)
2. ArrayAdapter(Context context, int resource, int textViewResourceId)
3. ArrayAdapter(Context context, int resource, T[] objects)
4. ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
5. ArrayAdapter(Context context, int resource, List<T> objects)
6. ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

Creating ArrayAdapter From Resource

Not only can arrayadapter be creating using constructors but it can also be created from an external resource.

Android provides a createfromresource() method which is static. This method returns us an ArrayAdapter instance.

Binding Lists and Arrays into an ArrayAdapter

You can bind a list of objects or an array into an ArrayAdapter via the constructor. For array:

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

For List:

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

EXAMPLE:

ArrayAdapter myArrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,galaxiesList)

Adding Individual Elements into an ArrayAdapter

Or you can add items individually to an already existing ArrayAdapter or a collection of elements into an existing ArrayAdapter.

To add an object:

myArrayAdapter.add(T object)

This will add the specified object to the end of our arrayadapter.

To add a collection:

myArrayAdapter.addAll(Collection<? extends T> collection)

This will add a Collection to the end of our adapter.

Or add a list that is not necessarily a collection, like say an array:

myArrayAdapter.addAll(T... items)

where T is an Object.

Clearing the ArrayAdapter

This will remove everything from the adapter:

myArrayAdapter.clear()

Getting the total number of items in the ArrayAdapter

We can get the count of items in the arrayadapter using the getCount() method:

int total = myArrayAdapter.getCount();

Getting a particular item from an ArrayAdapter

You can obtain item from an ArrayAdapter:

Object myItem = myArrayAdapter.getItem(position);

You pass the position, an integer.

Getting the Position of a Known Object

Sometimes you don't know the position of an object, but you know the object. So you can get the position by passing the item to the getPosition() method.

int itemPosition = myArrayAdapter.getPosition(item);

Comments