Adapter in Android (ListView, GridView and Spinner)

 Introduction

In the previous tutorial, you have learned about the menus in Android i.e. options menu, context menu, and pop-up menu in detail. In this tutorial, you will learn about the concept of ArrayAdapter. You will find the detailed answers to very broad questions that beginners ask when getting started with Adapters. What are Adapters? How to use Adapters? What is Adapter View? How to use Adapters with ListView, GridView, and Spinners?

Adapter

We use Adapter as to remove the incompatibility between the view and the data source i.e. incompatibility between the database and your application as Views are the parts of the Android Application.

Adapter acts as a bridge between AdapterView and the data for that specific view. The Adapter provides access to the data items as well as Adapter is also responsible for creating the View for each item of the DataSet.

Adapter in Android
Adapter in Android

What if you don’t want to use Android ArrayAdapter?

If you don’t want to use Android Adapter, you will miss a lot of features provided by Android Adapter. If you are planning not to use Android Adapter then you should keep in mind that

  • You must create TextViews in ScrollView Group so that you can scroll TextViews if the size increases.
  • You must implement the pagination for the content of the TextView.
  • You must implement the click events separately for each TextView.

ArrayAdapter

ArrayAdapters are commonly used for

  1. List View
  2. Grid View
  3. Spinner

Steps to Use Adapter with Adapter View

Follow the given steps to use Adapter with AdapterView.

  1. Create AdapterView in Android Activity as you add a View in Android Activity layout.
  2. Define the Data Source i.e. some Array or database.
  3. Create an Adapter i.e. ArrayAdapter. (You must specify the layout, context, and the string array for each layout)
  4. Get the reference to ArrayAdapter i.e. by using findViewById() from R (Resource) file.
  5. Set the Adapter on Adapter View i.e. by using setAdapter() method.
  6. Set the event listener as required i.e. by using onItemClick() method.

ListView with ArrayAdapter

You can use ListView when you want to display all the options in the form of a list. ListView in Android also allows multiple selections, so that user can select multiple items from the list and can perform some action by selected items.

Creating ListView

You can either drag and drop ListView from the palette, or you can create ListView in your XML file.

Define Data Source as Array

Initialize and declare a string array with the content of ListView.

Creating ArrayAdapter 

Declare a new ArrayAdapter of String type and populate ArrayAdapter from the String Array declared before.

Reference of ListView

Create a new ListView with reference of previously created ListView i.e. listView1.

Set Adapter on Adapter View

Item Click Listener for ListView Item Clicked

Implement the item click listener so that when the user clicks on any of the list items in the ListView, you can perform any action.

ListView with ArrayAdapter
ListView with ArrayAdapter

Complete code for ArrayAdapter with ListView

GridView with ArrayAdapter

You can use GridView when you want to show data in the form of Grid i.e. rows and columns. When using GridView you can set the number of columns in the Grid. It also allows multiple items selection.

Creating GridView

You can either drag and drop GridView from the palette, or you can create GridView in your XML file.

Define Data Source in strings.xml

Initialize and declare a string array with the content of GridView in the strings.xml file of the project.

Define Data Source 

Now in the Java file of the project, declare a String array for the GridView. (this string array must have class level scope i.e. outside all the methods but inside the class)

Get Values for the Array

From the resource file of the project get the values of the array i.e. declared in strings.xml file.

Create Adapter

Declare a new ArrayAdapter of String type and populate ArrayAdapter from the String Array declared before.

Reference of GridView

Create a new GridView with reference of previously created GridView i.e. gridView1.

Set Adapter on Adapter View

Item Click Listener for ListView Item Clicked

Implement the item click listener so that when the user clicks on any of the list items in the GridView, you can perform any action.

Gridview with ArrayAdapter
Gridview with ArrayAdapter

Complete code for ArrayAdapter with ListView

Multiple Selection of Items

If you want to enable multiple selections of the items, you can just add choice mode in your ListView XML.

In this case, you must also set your ArrayAdapter to multiple choice mode.

 

Spinner with ArrayAdapter

You can use spinner when you want to show a drop down list on a list item to click. Spinner occupies less space than traditional ListView.

Creating a Spinner

You can create a spinner by drag drop from the palette, or you can add spinner in you XML layout file manually.

Spinner Modes

Spinner provides two modes

  1. dialog: displays separate dialog box to show list items.
  2. dropdown: displays a drop down menu to show list items.

Define Data Source

In strings.xml file, create a new string array.

Define Data Source 

Now in the Java file of the project, declare a String array for Spinner. (this string array must have class level scope i.e. outside all the methods but inside the class)

Get Values for the Array

From the resource file of the project get the values of the array i.e. declared in strings.xml file.

Create Adapter

Declare a new ArrayAdapter of String type and populate ArrayAdapter from the String Array declared before.

Reference of Spinner

Create a new GridView with reference of previously created GridView i.e. gridView1.

Set Adapter on Adapter View

Item Select Event Listener for Spinner

Spinner Adapter
Spinner Adapter

Complete Code for Spinner with Adapter View

 

Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!