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. The basic syntax for ArrayAdapter is given as:
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Parameters
Parameters |
Description |
---|---|
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. |
textViewResourceId | 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. |
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, R.id.itemTextView, 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, R.id.itemTextView, courseList[]);
Example
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
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > < ListView android:id = "@+id/simpleListView" android:layout_width = "match_parent" android:layout_height = "wrap_content" /> </ RelativeLayout > |
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
<? 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:layout_gravity = "center" /> </ LinearLayout > |
Step 4: Working with the MainActivity.java file
Now go to the java folder and in MainActivity.java and provide the implementation to the ArrayAdapter. Below is the code for the MainActivity.java file.
Java
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" }; @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); } } |
Contact Us