Android Adapter Tutorial With Example | Types Of Adapter

Android Adapter Tutorial With Example In Android Studio is the topic of the day.

Android adapters are the bridges between the core data source and the android UI views like ListView, GridView or Spinner.

To quickly understand the basic functionality of android adapter, see the below image

android adapter
Let us go from Left to Right.

First of all, UI view like ListView, GridView or Spinner will request the data from the adapter.

Then adapter will fetch the data from the data source as per the blue arrow.

Now, adapter will pass this data to the ListView, GridView or Spinner which have requested the data as per the Orange arrow.

We can compare the mechanism of android adapter with the Electricity (data source) , Mobile (ListView etc.) and electric adapter.

Mobile device need electricity which it consumes via electric adapter.

You can say that mobile device is listview, electricity is a data source and electric adapter is the android adapter.

Methods of the Adapter

Following are the methods which are helping in the transportation of the data.

1. getCount()

How many items are in the data set represented by this Adapter.

It will return the number of items in the dataset.

For example, number of rows in the ListView.

2. getItem(int position)

Get the data item associated with the specified position in the data set.

3. getItemId(int position)

Get the row id associated with the specified position in the list.

4. getItemViewType(int position)

Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.

integer parameter is the position of the data set.

5. getView(int position, View convertView, ViewGroup parent) 

Get a View that displays the data at the specified position in the data set.

6. getViewTypeCount()

Returns the number of types of Views that will be created by getView(int, View, ViewGroup).

7. hasStableIds()

Indicates whether the item ids are stable across changes to the underlying data.

8. registerDataSetObserver(DataSetObserver observer)

Register an observer that is called when changes happen to the data used by this adapter.

9. unregisterDataSetObserver(DataSetObserver observer)

Unregister an observer that has previously been registered with this adapter via registerDataSetObserver(DataSetObserver).

Types of Adapters

  1. ArrayAdapter
  2. CustomArrayAdapter
  3. BaseAdapter
  4. SimpleAdapter
  5. CustomSimpleAdapter
  6. Cursor Adapter

ArrayAdapter

Whenever the data source is in the form of the ArrayList, this type of adapter is the best suitable option to use for ListView, GridView or Spinner.

By default, the array adapter creates a view by calling Object.toString() on each data object in the collection you provide, and places the result in a TextView.

You may also customize what type of view is used for the data object in the collection.

To customize what type of view is used for the data object, override getView(int, View, ViewGroup) and inflate a view resource.

Below is the sample usage of the Arrayadapter

This adapter will use the String array as the data source.

Second parameter is the XML layout file which represent the every view of the ListView.

Third one is the id of textview which is in the every row of ListView.

cityList is the string array. (Data Source)

ArrayAdapter can have below types of constructors as per the requirements.

1. ArrayAdapter(Context context, int resource)

2. ArrayAdapter(Context context, int resource, int textViewResourceId)

3. ArrayAdapter(Context context, int resource, T[] objects)

4. ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

5. ArrayAdapter(Context context, int resource, List<T> objects)

6. ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

Custom ArrayAdapter

When we want to add multiple UI widget like TextView, ImageView etc. in the listview, we need to use custom Arrayadapter.

To make Custom ArrayAdapter, we need to make one JAVA class, which extends the ArrayAdapter.

Arrayadapter can override all the methods of the Base Adapter.

Code example of custom arrayadapter is as the below

Base Adapter

As the name suggests, it is the common class which other adapters can use or extend.

Whenever the complex customization in needed in the ListView, GridView or Spinner, base adapter helps us to make our custom adapter with it’s methods.

Following is the code sample with the use of Base adapter

As you can see that there some methods like getViewTypeCount(), getItemViewType(), getCount(), getItem(), getItemId(), getView() etc.

Custom adapter can override these methods as per the custom requirements.

Simple Adapter

Whenever we have static data that are not updating with time, we can use simple adapter to populate them in listview or others.

Simple Adapter provides easy way to map this static data to XML views in quick time.

You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views. Binding data to views occurs in two phases.

Example of implementing simple adapter is as the below

First parameter is the context.

Second is the List of the Hashmaps.

Third one is the reference to the XML layout file which is making view for every row of listview.

Fourth is the string array. Fifth is the integer array.

Following is the definitions of the parameters.

Custom Simple Adapter

As we have seen, we can use simple adapter when we have static data.

But when there are dynamic data which are updating frequently, then we need to make custom simple adapter.

A JAVA class which extends the simple adapter can be defined as the custom simple adapter.

For making click events of the children of the listview row item, we also require custom simple adapter. Simple adapter can not handle click events.

Following can be the possible implementation of the Custom Simple Adapter

You can read that JAVA class is extending the SimpleAdapter.

Here, you can override all the methods of Base Adapter for particular requirements.

Cursor Adapter

We need to use Cursor Adapter when we want to display the data from an SQLite database query.

CursorAdapter makes it easy to use when the resource of a listview is coming from database and you can have more control over the binding of data values to layout controls.

Following is the code example for Cursor adapter.

In the newView() method, you simply inflate the view and return it. This view is the XML layout file for listview row item.

In the bindView() method, you set the elements of your view. You can set the text to textview or set imageview in this method.

Example of Adapter

Now let us make a listview example to actually see, how does adapter provides data to listview from the data source.

Make a new android studio project with empty activity as a first activity.

In the activity_main.xml file, add the below source code

There is only one listview in the main XML file.

Make a new file in the res->layout directory and give it a name lv_item.xml

Write down the following coding lines in lv_item.xml file

This file will generate the view layout for every single row of the listview.

This is a common file and listview will use while creating every child row.

One textview and one imageview are present in this file.

Time to add images to the res->drawable directory.

Download the images by clicking the below link

demonuts_images_listview

Add these images into res->drawable directory.

Following is the code for MainActivity.java file

First of all, add the required images to the res->drawable folder.

One integer array called myImageList contains the drawable resources to the images.

String array myImageNameLiist contains the names of the images in the string format.

I have used simple adapter in this file to attach it to the listview.

Above example will give you the following output

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...

2 Responses

  1. Priyanka says:

    Nice one..!!!!

Leave a Reply

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