Android Adapters – The art of accessing the data items in Android

An adapter basically connects the User Interfaces and the Data Source. According to Android officials, “An Adapter object acts as a bridge between an AdapterView and the data for that view. Android Adapters basically provides access to the data items.”

So we know, an adapter implements the Adapter Interface and acts as a link between a data set and an adapter view. An AdapterView is an object of a class extending the abstract AdapterView class. Now when we talk about the data, it can be anything that is present in Structured Manner. It also retrieves data from data set & generates view objects based on that data. It shows the data in different views like GridView, ListView, Spinners, etc.working of android adapter

Keeping you updated with latest technology trends, Join DataFlair on Telegram

Adapter View in Android

An Adapter View displays the set of data in the form of List or Grid provided by the Adapter. It has the capability to display a large number of items on the User Interface efficiently. An Android Adapter is responsible for taking the data from the source and put it in the AdapterView. And it is the responsibility of AdapterView to display the data. Adaptors help us make the user interface interactive and friendly to use.

Learn to make UI interactive by using UI Controls with DataFlair.

An android adapter vier can display the data on the Display screen in three forms that are:android adapter views

Let us see these Views one by one:

1. ListView

The speciality of ListView is that it shows the items in a Horizontal form, here each item lies below the other. These items are displayed in Vertically- Scrollable collection. The example is given below:android list view

2. GridView

In a Grid View, each item is displayed in a tabular form or a 2-Dimensional view, that is in rows and columns. This is also a vertically-scrollable view. To understand this view, consider the following image:android grid view

3. Spinner

Spinner view is a different view when we talk about all three. In Spinner, we get to choose a value from a list of Values. This is like a dropdown menu that has a set of values from which the user can choose one.android spinners

Types of Android Adaptersandroid adapters types

Android provides us with following different types of Adapters that are used to fill the data in UI components:

  • BaseAdapter – BaseAdapter is the parent adapter for the rest of the Adapters.
  • CursorAdapter – This adapter makes it easy and more controlled to access the binding of data values.
  • ArrayAdapter – ArrayAdapter presents the items in a single list backed by an array.
  • Custom ArrayAdapter – It displays the custom list of an Array.
  • SimpleAdapter – SimpleAdapter is an easy adapter to map static data to the views through the XML file.
  • Custom SimpleAdapter – It displays a customized list and enables us to access the child data of the list or grid view.

Now, we will see all these Android Adapters one by one:

1. BaseAdapter in Android

Base Adapter is a general implementation of an Adapter that is extended in other Android Adapters for their use.

Other android adapters extend it to create a custom Adapter for displaying a common custom list item. To extend a BaseAdapter in a customized adapter we extend it as follows:

public class My_Adapter extends BaseAdapter{
        //overridden methods
        public int getCount(){
        //code
        }
        public Object getItem(int i) {
        //code
        }
        // other methods
}

2. ArrayAdapter in Android

Whenever we need to arrange the data in ArrayList, we use the ArrayAdapter. The array adapter uses Object.toString() for each data object to create a view, and then place the result in a TextView. This type of android adapter is used for ListView, GridView or Spinner.

To include it we can use ArrayAdapter as follows:

ArrayAdapter ( Context context, int resource, int textViewResourceId, Item[] objects)

ArrayAdapter is a class and can have various types of Constructors, those constructors are mentioned below:

  • ArrayAdapter( Context context, int resource)
  • ArrayAdapter( Context context, int resource, int textViewResourceId)
  • ArrayAdapter( Context context, int resource, T[] objects)
  • ArrayAdapter( Context context, int resource, textViewResourceId, T[] objects)
  • ArrayAdapter( Context context, int resource, List<T> objects)
  • ArrayAdapter( Context context, int resource, int textViewResourceId, List<T> objects)

3. Custom ArrayAdapter in Android

We implement a Custom ArrayAdapter when we want to add many UI widgets like EditText, TextViews, etc in the list and all at a time. To use a Custom ArrayAdapter, we need to create a Java class that will extend ArrayAdapter. This ArrayAdapter can override the methods of the BaseAdapter.

To see how we can do it, we see the following:

Public class MyCustomAdapter extends ArrayAdapter{
        Public CustomAdapter( Context context, int resource, int ResourceId, List objects) {
        }
  //override other methods
        }

4. CursorAdapter in Android

We use CursorAdapter when we need to display some data using the SQLite database query. This Android Adapter enables us to use the resources easily in ListView.

To use it we can do the following:

public class MyCursorAdapter extends CursorAdapter {
 // Constructor and required methods 
}

5. SimpleAdapter in Android

A simple Adapter is used for static data that does not change dynamically. It provides an easy way to add static data in a view that are defined in the XML file. We can add it using the following:

SimpleAdapter (Context context, List<? Extends Map<String, ?>> data, int resource, String[] from, int[] to)

6. Custom SimpleAdapter in Android

We use CustomSimpleAdapter to implement custom Adapters. To implement a list of items backed by an Array we need to use ArrayAdapter. So, to customize a ListView or a GridView we would need to implement SimpleAdapter first. Further, if we want more customization in the Items where we have many items then we need to implement the Custom SimpleAdapter.

Any doubts in Android Adapters article till now? Ask in the comment section.

Methods of Android Adaptersmethods of android adapters

There are certain methods of Adapter class that are mentioned below:

  • getCount() – This method represents the number of items present in the data set.
  • hasStableIds() – This method indicates whether the Item Ids are stable under changes on the underlying data.
  • getItem(int position) – This method gets the data item associated with a particular position in the data set.
  • getItemId(int position) – This method gets the Id of the item associated with a particular position in the list.
  • getViewTypeCount() – This method returns the number of types of Views created using getView().
  • getItemViewType(int position) – This method gets the type of View that would be created using the getView() method.
  • getView(int position, View convertView, ViewGroup parent) – This method displays the data at a position in the data set.
  • registerDataSetObserver(DataSetObserver observer) – This method registers the observer that is called when the changes in the data occur.
  • unregisterDataSetObserver(DataSetObserver observer) – This method unregisters the observer that was registered using the registerDataSetObserver(DataSetObserver observer) method.

Implementation of Adapters in Android

Now we will implement it in our application using the following steps:

Step 1. First of all, we will create a new project and name it. I have named my application “My Adapter”.

Step 2. Next we will create the layout for it in the activity_main.xml file:

<LinearLayout       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"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <TextView
       android:id="@+id/tV1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_marginLeft="90dp"
       android:layout_marginTop="40dp"
       android:fontFamily="@font/amarante"
       android:text="DataFlair "
       android:textColor="@color/colorPrimaryDark"
       android:textSize="50dp" />

   <ListView
       android:id="@+id/Dog_List"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></ListView>

</LinearLayout>

Step 3. We will write the following code in the MainActivity.java file.

package com.DataFlair.myadapter;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   String[] Dogs = {"Husky", "Labrador", "Chow Chow", " Beagle ", " Rottweiler ", " Doberman ", "Chihuahua", " Pit Bull ", "Pug", "Golden Retriever ", "German Shepherd", "Great Dane", "Boxer"};

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       ArrayAdapter my_adapter = new ArrayAdapter<String>(this,
               R.layout.my_list, Dogs);

       ListView List = findViewById(R.id.Dog_List);

       List.setAdapter(my_adapter);
   }
}

Step 4. Now we will create a file to customize the listview that would be shown. For that create a file my_list.xml and write the following code.

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/customize"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:fontFamily="@font/amiri"
   android:padding="10dp"
   android:textColor="#4A0505"
   android:textSize="20dp"
   android:textStyle="bold">

</TextView>

Step 5. After writing this, we will run the code:

i. This is the ListView.list view in android

ii. This list is scrollable.scrollable list

Summary

So in this article, we have read about the Adapters in Android. We saw what are android adapters, and their types. We also went through the methods that are required to implement Android Adapters. In the end, we also implemented Adapter through ListView in our application. We will read them in detail in the coming articles.

Learn to build your first Android App with DataFlair.

I hope you liked DataFlair’s Android Adapters article. Do refer our sidebar for more Android Tutorials.

Happy Learning😃

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.