Android Custom ArrayAdapter Tutorial With Example In Android Studio

Welcome guys to Android Custom ArrayAdapter Tutorial With Example In Android Studio.

Android adapter gives facility to transfer the data from the data source to multi views like listview, gridview or spinner.

There are few types of adapter. Base adapter, simple adapter, custom simple adapter, array adapter, custom arrayadapter etc.

Array adapter is used when the data source is available in the list format. For example, string array.

With array adapter, we can develop the listview with limited functionalities.

For creating more complex structures, developers can use custom array adapter.

To make custom arrayadapter, we need to develop specific JAVA class which extends the arrayadapter.

After extending the array adapter, we can override the methods of base adapter to customize the adapter.

We can override the methods of base adapter because array adapter is extending base adapter also.

Sample code block for custom array adapter may look like the below

When the class is extending the ArrayAdapter then it is custom array adapter.

You should define the constructor of this class with super keyword as per shown int the above code.

Array Adapter is the sub class of the base adapter, so you can override the methods of the base adapter for more customization.

Methods of Custom Array Adapter

Now Let us read the methods available in the above code one by one.

1. getView()

This method helps us to create the each view for the row item of listview or cell of gridview.

super.getView() method can inflate the view for row item.

Using this inflated view, we can get the access to the various UI widgets present in the row item XML file.

For example, I have inflate the textview using it’s id and also have implemented it’s click scenario.

Similarly, you can access other UI widgets as per your necessity.

2. getCount()

This method will return the number of rows present in the listview.

It will give us number of cells in the case of gridview.

We can use data source like arraylist to check the total number of data items and then return this number.

3. getItem(int position)

This method will return the data at the specified position.

For example, a hashmap from arraylist of hashmap at the specified position will be returned by this method.

4. getItemId()

This method will return the specific id associated with the data set.

5. getItemViewType(int position)

Listview can have more than two types of row.

For example, a header row, sub header row, normal child row, footer row etc.

When compiler is creating the every row of listview, there should be something which tells it type of the row.

This work will be done by the getItemViewType method.

Custom Array Adapter Example

Prepare a new android studio project.

Step 1. Layout Files

Now in the activity_main.xml file, add the following code lines

I have added one listview in the main layout file.

This listview have two properties about divider.

Divider height and color. You can change it with different values as per your mind.

Now make a new XML resource file under res->layout directory.

Name of this file should be custom_item.xml

Coding lines for custom_item.xml are as the following

Adapter will use this file to make the view of the each row of the listview.

This file has one textview. Adapter will set different text value in different row.

Step 2. Custom Array Adapter

Make a new JAVA class and give it a name

Code structure for this file is as the below

As you can see that we have created a custom array adapter class.

This class is extending an ArrayAdapter.

Constructor of this array has three parameters.

First is context. Second is layout XML file for row (which is custom_item.xml) and third is string array, which is our data source.

Now getView() method will create the layout for each row of the listview.

In this method, compiler will first inflate the  custom_item.xml file.

Then it will find the textview using it’s id.

After that, it will set the text to the textview using data source. (string array myImageArray)

Then after, it will write click event for this textview.

Step 3. Main JAVA file

At last, in the file, write down the below source code

First of all, compiler will create the data set, a string array with names of cars in the string format.

Then it will create the object of the CustomArrayAdapter class then it will set this object to the listview.

View for android custom arrayadapter ListView

android custom arrayadapter

Custom ListView

android custom arrayadapter

Click Event

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 *