Introduction
In the previous tutorial, you have learned about the menus in Android i.e. options menu, context menu, and pop-up menu in detail. In this tutorial, you will learn about the concept of ArrayAdapter. You will find the detailed answers to very broad questions that beginners ask when getting started with Adapters. What are Adapters? How to use Adapters? What is Adapter View? How to use Adapters with ListView, GridView, and Spinners?
Adapter
We use Adapter as to remove the incompatibility between the view and the data source i.e. incompatibility between the database and your application as Views are the parts of the Android Application.
Adapter acts as a bridge between AdapterView and the data for that specific view. The Adapter provides access to the data items as well as Adapter is also responsible for creating the View for each item of the DataSet.

What if you don’t want to use Android ArrayAdapter?
If you don’t want to use Android Adapter, you will miss a lot of features provided by Android Adapter. If you are planning not to use Android Adapter then you should keep in mind that
- You must create TextViews in ScrollView Group so that you can scroll TextViews if the size increases.
- You must implement the pagination for the content of the TextView.
- You must implement the click events separately for each TextView.
ArrayAdapter
ArrayAdapters are commonly used for
- List View
- Grid View
- Spinner
Steps to Use Adapter with Adapter View
Follow the given steps to use Adapter with AdapterView.
- Create AdapterView in Android Activity as you add a View in Android Activity layout.
- Define the Data Source i.e. some Array or database.
- Create an Adapter i.e. ArrayAdapter. (You must specify the layout, context, and the string array for each layout)
- Get the reference to ArrayAdapter i.e. by using findViewById() from R (Resource) file.
- Set the Adapter on Adapter View i.e. by using setAdapter() method.
- Set the event listener as required i.e. by using onItemClick() method.
ListView with ArrayAdapter
You can use ListView when you want to display all the options in the form of a list. ListView in Android also allows multiple selections, so that user can select multiple items from the list and can perform some action by selected items.
Creating ListView
You can either drag and drop ListView from the palette, or you can create ListView in your XML file.
1 2 3 4 5 6 7 |
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </ListView> |
Define Data Source as Array
Initialize and declare a string array with the content of ListView.
1 |
final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; |
Creating ArrayAdapter
Declare a new ArrayAdapter of String type and populate ArrayAdapter from the String Array declared before.
1 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, months); |
Reference of ListView
Create a new ListView with reference of previously created ListView i.e. listView1.
1 |
ListView listview = (ListView) findViewById(R.id.listView1); |
Set Adapter on Adapter View
1 |
listview.setAdapter(adapter); |
Item Click Listener for ListView Item Clicked
Implement the item click listener so that when the user clicks on any of the list items in the ListView, you can perform any action.
1 2 3 4 5 6 |
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(getApplicationContext(), months[position], Toast.LENGTH_SHORT).show(); } }); |

Complete code for ArrayAdapter with ListView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.example.muneeb.listviewadapter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, months); ListView listview = (ListView) findViewById(R.id.listView1); listview.setAdapter(adapter); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(getApplicationContext(), months[position], Toast.LENGTH_SHORT).show(); } }); } } |
GridView with ArrayAdapter
You can use GridView when you want to show data in the form of Grid i.e. rows and columns. When using GridView you can set the number of columns in the Grid. It also allows multiple items selection.
Creating GridView
You can either drag and drop GridView from the palette, or you can create GridView in your XML file.
1 2 3 4 5 6 7 8 |
<GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:numColumns="3" > </GridView> |
Define Data Source in strings.xml
Initialize and declare a string array with the content of GridView in the strings.xml file of the project.
1 2 3 4 5 6 7 8 9 |
<string-array name="days_array"> <item>Monday</item> <item>Tuesday</item> <item>Wednesday</item> <item>Thursday</item> <item>Friday</item> <item>Saturday</item> <item>Sunday</item> </string-array> |
Define Data Source
Now in the Java file of the project, declare a String array for the GridView. (this string array must have class level scope i.e. outside all the methods but inside the class)
1 |
String[] days; |
Get Values for the Array
From the resource file of the project get the values of the array i.e. declared in strings.xml file.
1 |
days = getResources().getStringArray(R.array.days_array); |
Create Adapter
Declare a new ArrayAdapter of String type and populate ArrayAdapter from the String Array declared before.
1 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, days); |
Reference of GridView
Create a new GridView with reference of previously created GridView i.e. gridView1.
1 |
GridView gridview = (GridView) findViewById(R.id.gridView1); |
Set Adapter on Adapter View
1 |
gridview.setAdapter(adapter); |
Item Click Listener for ListView Item Clicked
Implement the item click listener so that when the user clicks on any of the list items in the GridView, you can perform any action.
1 2 3 4 5 6 |
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(getApplicationContext(), "You Clicked: " + days[position], Toast.LENGTH_SHORT).show(); } }); |

Complete code for ArrayAdapter with ListView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.example.muneeb.listviewadapter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { String[] days; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); days = getResources().getStringArray(R.array.days_array); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, days); GridView gridview = (GridView) findViewById(R.id.gridView1); gridview.setAdapter(adapter); gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(getApplicationContext(), "You clicked: " + days[position], Toast.LENGTH_SHORT).show(); } }); } } |
Multiple Selection of Items
If you want to enable multiple selections of the items, you can just add choice mode in your ListView XML.
1 |
android:choiceMode="multipleChoice" |
In this case, you must also set your ArrayAdapter to multiple choice mode.
1 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, months); |
Spinner with ArrayAdapter
You can use spinner when you want to show a drop down list on a list item to click. Spinner occupies less space than traditional ListView.
Creating a Spinner
You can create a spinner by drag drop from the palette, or you can add spinner in you XML layout file manually.
1 2 3 4 5 6 7 8 |
<Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinner1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> |
Spinner Modes
Spinner provides two modes
- dialog: displays separate dialog box to show list items.
- dropdown: displays a drop down menu to show list items.
Define Data Source
In strings.xml file, create a new string array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<string-array name="sports_array"> <item>Badminton</item> <item>Rugby</item> <item>Squash</item> <item>Tennis</item> <item>Soccer</item> <item>Field Hockey</item> <item>Cricket</item> <item>Basketball</item> <item>Ice Hockey</item> <item>Baseball</item> <item>Cycling</item> <item>Football</item> </string-array> |
Define Data Source
Now in the Java file of the project, declare a String array for Spinner. (this string array must have class level scope i.e. outside all the methods but inside the class)
1 |
String[] sports; |
Get Values for the Array
From the resource file of the project get the values of the array i.e. declared in strings.xml file.
1 |
sports = getResources().getStringArray(R.array.sports_array); |
Create Adapter
Declare a new ArrayAdapter of String type and populate ArrayAdapter from the String Array declared before.
1 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, sports); |
Reference of Spinner
Create a new GridView with reference of previously created GridView i.e. gridView1.
1 |
Spinner spinner = (Spinner) findViewById(R.id.spinner1); |
Set Adapter on Adapter View
1 |
spinner.setAdapter(adapter); |
Item Select Event Listener for Spinner
1 2 3 4 5 6 7 8 9 10 11 12 |
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(getApplicationContext(), "You Clicked: " + sports[pos], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); |

Complete Code for Spinner with Adapter View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.example.muneeb.spinneradapter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends AppCompatActivity { String[] sports; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sports = getResources().getStringArray(R.array. sports_array); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, sports); Spinner spinner = (Spinner) findViewById(R.id.spinner1); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(getApplicationContext(), "You Clicked: " + sports[pos], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } } |
Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!