Array Adapter and Example
Array Adapter and Example
Module 3
What is an Array adapter
• 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.
What is an Array adapter
• 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 Array adapter
public ArrayAdapter(Context context, int resource, int
textViewResourceId, T[] objects)
Parameters Description
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.
Parameter Explanation - 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.
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[]);
Using an Arrayadapter with a ListView
• In Android development, any time we want to show a vertical
list of scrollable items we will use a LisView which has data
populated using an Adapter.
• The simplest adapter to use is called an ArrayAdapter because
the adapter converts an ArrayList of objects into View items
loaded into the ListView container.
Using an Arrayadapter with a ListView
The ArrayAdapter fits in between an ArrayList (data source) and the ListView (visual representation) and
configures two aspects:
Which array to use as the data source for the list
How to convert any given item in the array into a corresponding View object
Note as shown above that there are other data sources besides an ArrayAdapter such as the CursorAdapter
which instead binds directly to a result set from a Local SQLite Database.
ArrayAdapter Simple 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
• Create a new Android Project and 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.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout code for
xmlns:android="http://schemas.android.com/apk/res/an the activity_main.
droid"
xmlns:tools="http://schemas.android.com/tools" xml file.
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 version="1.0" encoding="utf-8"?>
<LinearLayout code for the
xmlns:android="http://schemas.android.com/apk/res/an item_view.xml.
droid"
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>
import android.os.Bundle;
Step 4 Working with the import android.widget.ArrayAdapter;
import android.widget.ListView;
ListView simpleListView;
Now go to the java folder
and in MainActivity.java and // array objects
provide the implementation String courseList[] = {"C-Programming", "Data Structure", "Database", "Python",
to the ArrayAdapter. "Java", "Operating System", "Compiler
Design", "Android Development"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);