Android ArrayAdapter Tutorial With Example | ListView Spinner

Learn about Android ArrayAdapter Tutorial With Example in this article.

In android, adapter is the connector between the data source and the multi dynamic views like listview, gridview or spinner.

Adapter will fetch the data from data source like arraylist or hashmap.

Then it will provide this data to the listview, gridview or spinner using appropriate in built methods and constructors.

Adapter can be of various types as per it’s functionalities and core structure.

Arrayadapter, simple adapter, cursor adapter (for fetching data from the database) etc. are the basic adapters.

Using above adapters you can implement simple listview, spinner and gridviews.

For having complex functionalities like click events, checkbox maintenance, radio button etc. we need to make custom adapters.

Today, we will learn about basic arrayadapter and it’s functionalities.

Arrayadapter Information

Arrayadapter is one of the adapter that is used for making the simple types of multi views.

Whenever you have a list of single type of items which is backed by an array, you can use ArrayAdapter. For instance, list of phone contacts, countries or names.

Basically, arrayadapter accepts the simple row layout like single Textview for listview.

For more complex structure you need to make custom arrayadapter.

Arrayadapter can use data sets like string array or arraylist of hashmap to populate the listview.

Following is the constructor demonstration for Arrayadapter.

Arrayadapter has also different implementation of constructor.

Above one is the basic and generally, developers prefers the above constructor for easy coding.

Now let us understand the parameters of the constructor in deep.

1. Context context

context gives the current reference of the app to the constructor.

Reference means that context will tell the compiler that through the whole android app, in which activity of fragment we are using the adapter.

Developers use Activity.this, this, getActivityContext(), getActivity() etc. options to provide the context.

2. int resource

This parameter is the reference for the XML file, which is making the layout for row items in listview or spinner.

In arrayadapter, you can make a separate XML file which a textview. Adapter will use this file for making the row layout structure.

You can change the text color, size, style etc. in the textview of this file.

While creating the constructor, second parameter need the value as this XML file.

For example, see the below code line

As you can see that second parameter pointing to the list_item.xml file.

3. int textViewResourceId

It is pointing to the id of the textview, in which text will be written.

If we have made a separate XML file, then we can give the id of the textview present in that separate XML file.

For example,

Here, our file name is list_item.xml. In this file, we have one textview.

Id of this textview is tvCars. So third parameter has the value “R.id.tvCars.

Sometimes, we may not create separate XML file for arrayadapter.

In this case, we can use something like  android.R.id.text1

This value is the in built id of the textview.

Second parameter is also the android system generated value.

4. T[] objects

This parameter is the main data source.

Adapter will use this data source to populate the listview or spinner.

T[] means that arraylist of any objects.

For example, arraylist of string variables or arraylist of hashmaps etc.

See the below implementation

First line is the string array. This array includes the names of the vehicle company in the string format.

Now you can directly write the name of the string array as the fourth parameter of the constructor as per the second line.

String array is the primary data source in this example.

Examples of Arrayadapters

We will develop two examples using array adapter : with separate XML file for listview and without separate file.

First let us create the listview with separate file.

Step 1. XML writings

In the activity_main.xml file, add the following

Just one listview in the main layout file.

Now create a new XML layout resource file in res->layout directory.

Name of this file should be array_list_item.xml

Write the below source code in array_list_item.xml file

There is one textview and one simple view is there.

Textview will get the names from the data source while simple view will work as the divider of listview.

Step 2. Code for Main file

In the MainActivity.java file, add the below code

I have written one string array “myImageNameList.”

This string array includes the names of the vehicle companies.

Now let us see the arrayadapter code.

I have make a new object of array adapter. First parameter is context.

Second is the reference to the array_list_item.xml file.

Third parameter holds the id of the textview and last one is the data source which is our string array.

Following images are the output of this example.

android arrayadapter

Second Example

We will make this example with any new XML file.

So make a new project in the android studio with empty activity.

In the activity_main.xml file, add the following

Again, just one listview in the main xml file.

In the MainActivity.java file, write the below lines

As you can see that it is the same as the above example.

Just one change is there in the arrayadapter object creation.

In the second and third parameter, android’s in built resources are used instead of separate XML file and textview.

Output of above example

android arrayadapter

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *