Android ArrayAdapter

android arrayadapter

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)


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:


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);

ArrayAdapter Examples

1. Android ArrayAdapter - ListView and ArrayList

Android ArrayAdapter - ListView and ArrayList tutorial.

Adapters like ArrayAdapter help bridge the gap between model data and the adapterview,two very different stuff with different responsibilities.Our model data represents our data source.

It maybe from a database or a simple data collection,like say array or arraylist.The adapterview represents the visual representations.They are view children hence are concerned with showing data.

Adapterviews include ListViews,GridViews and spinner. Our adapter shall help link these two. ArrayAdapters work with an array or java.util.List instance.

It takes :

  1. Context.
  2. Resource ID of the view to be used in the adapterview.
  3. List of data to be bound.

What we do :
  • Fill our adapterview with data.
  • Our adapterview in this case is a simple ListView.
  • We use ArrayAdapter as our adapter.
  • Our data source is an arraylist.
  • So we instantiate and set to our listview using the setAdapter() method.
  • While instantiating our adapter we need to pass a context,layout and data source.
  • We handle our itemclicks and show a simple toast message.
What You do :
  • Create a project in android studio.
  • Give it a name and choose minimum and target SDKs.

SECTION 1 : Our Dependencies


  • Android Studio has added for us dependencies for AppCompat and Design support libraries.
  • Note we are subclassing AppCompatActivity to make our MainActivity class an activity.
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile ''

Our MainActivity

Main Responsibility : LAUNCH OUR APP.

  • It extends AppcompatActivity hence is an activity.
  • Activities are the entry point of android applications.This is no exception.
  • It loads our activity layout.
  • Our activity layout has our ListView.
  • We therefore reference that ListView and set its adapter.
  • We first instantiate our arrayadapter,passing in context,layout and data source.
  • Our arrayadapter is of generic type string.
  • To set our adapter to listview we use the setAdapter() method.
  • Our data source is a simple arraylist.
    package com.tutorials.hp.listviewarraylist;

    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {

        ArrayList<String> numbers=new ArrayList<>();
        ListView lv;

        protected void onCreate(Bundle savedInstanceState) {

            lv = (ListView) findViewById(;

            //FILL DATA

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, numbers);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Toast.makeText(MainActivity.this, numbers.get(i), Toast.LENGTH_SHORT).show();

        //FILL DATA
        private void fillData()
            for (int i=0;i<10;i++)
                numbers.add("Number "+String.valueOf(i));

SECTION 3 : Our Layouts

ActivityMain.xml Layout.
  • Inflated as our activity's view.
  • Add ListView here.
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android=""


Other Adapter Types

Let's look at some of the available adapter subclasses.

Android PagerAdapter Tutorial and Examples.

Normally you use PagerAdapters with ViewPager. ViewPager you know allows us swipe through pages.

Well PagerAdapter acts as the base class for providing the adapter to populate pages inside of that ViewPager.

ListAdapter is an interface that's used to bind a ListView to its data.

This inteface resides inside the android.widget package:

package android.widget;

It derives from android.widget.Adapter, an interface that contains several signatures to be implemented by it's implementers.


BaseAdapter as the name suggests BaseAdapter is a base adapter, or super adapter. An adapter is a class that acts as a bridge between an adapterview and the underlying data source.

SpinnerAdapter is an interface that binds android.widget.Spinner to its data.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post