Skip to content
Related Articles

Related Articles

ArrayAdapter in Android with Example
  • Last Updated : 23 Oct, 2020

The Adapter acts as a bridge between the UI Component and the Data Source. It converts data from the data sources into view items that can be displayed into the UI Component. Data Source can be Arrays, HashMap, Database, etc. and UI Components can be ListView, GridView, Spinner, etc. ArrayAdapter is the most commonly used adapter in android. When you have a list of single type items which are stored in an array you can use ArrayAdapter. Likewise, if you have a list of phone numbers, names, or cities. ArrayAdapter has a layout with a single TextView. If you want to have a more complex layout instead of ArrayAdapter use CustomArrayAdapter. The basic syntax for ArrayAdapter is given as:

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




contextThe current context. This value can not be null.
resourceThe resource ID for the layout file containing a layout to use when instantiating views.
textViewResourceIdThe id of the TextView within the layout resource to be populated.
objectsThe objects to represent in the ListView. This value cannot be null.

context: It is used to pass the reference of the current class. Here ‘this’ keyword is used to pass the current class reference. Instead of ‘this’ we could also use the getApplicationContext() method which is used for the Activity and the getApplication() method which is used for Fragments.

public ArrayAdapter(this, int resource, int textViewResourceId, T[] objects)

resource: It is used to set the layout file(.xml files) for the list items.

public ArrayAdapter(this, R.layout.itemListView, int textViewResourceId, T[] objects)

textViewResourceId: It is used to set the TextView where you want to display the text data.

public ArrayAdapter(this, R.layout.itemListView,, T[] objects)

objects: These are the array object which is used to set the array element into the TextView.

String courseList[] = {“C-Programming”, “Data Structure”, “Database”, “Python”,

                      “Java”, “Operating System”,”Compiler Design”, “Android Development”};

ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.itemListView,, courseList[]);


In this example, the list of courses is displayed using a simple array adapter. Note that we are going to implement this project using the Java language.

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Go to the layout folder and in activity_main.xml file change the ConstraintLayout to RelativeLayout and insert a ListView with id simpleListView. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
        android:layout_height="wrap_content" />

Step 3: Create a new layout file

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view.xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.


<?xml version="1.0" encoding="utf-8"?>
        android:layout_gravity="center" />

Step 4: Working with the file

Now go to the java folder and in and provide the implementation to the ArrayAdapter. Below is the code for the file. 


import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
    ListView simpleListView;
    // array objects
    String courseList[] = {"C-Programming", "Data Structure", "Database", "Python",
                            "Java", "Operating System", "Compiler Design", "Android Development"};
    protected void onCreate(Bundle savedInstanceState) {
        simpleListView = (ListView) findViewById(;
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                R.layout.item_view,, courseList);

Output: Run on Emulator


Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
My Personal Notes arrow_drop_up
Recommended Articles
Page :