Site icon DataFlair

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

adapters in android

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

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.

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:

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:

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:

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.

Types of Android Adapters

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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

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:

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 Adapters

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

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.

ii. This list is scrollable.

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😃

Exit mobile version