ArrayAdapter in Android with Example
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.
Syntax of ArrayAdapter
val adapter = ArrayAdapter(context, resource, textViewResource, objects)
Parameters:
- context: The current context. This value can not be null.
- resource: The resource ID for the layout file containing a layout to use when instantiating views.
- textViewResource: The id of the TextView within the layout resource to be populated.
- objects: The objects to represent in the ListView. This value cannot be null.
Example:
private var courseList: Array<String> = arrayOf(
“C-Programming”, “Data Structure”, “Database”, “Python”,
“Java”, “Operating System”, “Compiler Design”, “Android Development”
)
val arrayAdapter = ArrayAdapter(this, R.layout.item_view, R.id.itemTextView, courseList)
Step by Step Implementation of ArrayAdapter
In this example, the list of courses is displayed using a simple array adapter.
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 we are going to implement this project using both Java and Kotlin 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.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<ListView
android:id="@+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout:
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.
item_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/itemTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="24dp"
android:gravity="center" />
</LinearLayout>
Step 4: Working with the MainActivity file
Now go to the java folder and in MainActivity and provide the implementation to the ArrayAdapter. Below is the code for the MainActivity file.
MainActivity File:
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ListView simpleListView;
// array objects
String courseList[] = {"C-Programming", "Data Structure", "Database", "Python",
"Java", "Operating System", "Compiler Design", "Android Development"};
// Overriding the OnCreate Method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleListView = (ListView) findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
R.layout.item_view, R.id.itemTextView, courseList);
simpleListView.setAdapter(arrayAdapter);
}
}
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var simpleListView: ListView
// array objects
private var courseList: Array<String> = arrayOf(
"C-Programming", "Data Structure", "Database", "Python",
"Java", "Operating System", "Compiler Design", "Android Development"
)
// Override the OnCreate Method
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Loading the Method
simpleListView = findViewById(R.id.simpleListView)
// ArrayAdapter Instance
val arrayAdapter = ArrayAdapter(this,
R.layout.item_view, R.id.itemTextView, courseList
)
simpleListView.adapter = arrayAdapter
}
}
Output:

For learning more about the topic refer to these articles: