Free Android app covering all aspects of addiction, including prevention and treatment

Android Adapters: What are Adapters?

  • Written by  Clive

What are Adapters?

adapters icon

Adapters are the link between a set of data and the AdapterView that displays the data.

AdapterView

AdapterViews are ViewGroups that display child views given to it by an adapter. An example of an AdapterView is a ListView.

Adapters also provide the child views that display the data in the AdapterView. Adapters are responsible for supplying the data and creating the views representing each item.

Android Adapter Diagram

Adapters get the data and pass it, along with a child view, to the parent AdapterView which displays the child view and the data

The Android Framework has a set of native adapters that are easy to use. You can also create your own custom adapters if you wish.

Two of Android’s common adapters are:

ArrayAdapter

An ArrayAdapter is an adapter backed by an array of objects. It links the array to the Adapter View.

The default ArrayAdapter converts an array item into a String object putting it into a TextView. The text view is then displayed in the AdapterView (a ListView for example).

When you create the adapter, you need to supply the layout for displaying each array string. You can define your own or use one of Android’s, such as:

android.R.layout.simple_list_item_1

There are alternative constructors that you can use for more complex layouts. You can also display images instead of strings.

Here's a tutorial where we use a custom ArrayAdapter to display a list of images and text, Using Bitmaps efficiently.

SimpleCursorAdapter

The SimpleCursorAdapter links the data contained in a Cursor to an Adapter View.

Cursors

A cursor is a set of data. You usually get a cursor when you do a database query. The result of your query is contained in the cursor.

The SimpleCursorAdapter binds the Cursor data to an Adapter View. You define a layout that controls how each row of data is displayed.

Each row’s view is populated using the column values of the corresponding row in the cursor.

This layout is then displayed in the Adapter View, like a ListView for example.

Android Adapter Diagram SimpleCursorAdapter

The SimpleCursorAdapter gets the data out of the Cursor, puts each row of data in a layout that you define and then displays it in the Adapter View

When you construct the SimpleCursorAdapter, you specify which column’s data is to be retrieved from the cursor. You also specify which fields in your layout are to display this data. The adapter then creates a new view for each cursor entry and populates it with the corresponding cursor column values.

Other useful Adapters

CursorAdapter

A CursorAdapter links a Cursor’s data to a List View. You must include the database’s _id column as it’s used in processing the list item’s selection.

The SimpleCursorAdapter is a subclass of CursorAdapter.

The SimpleCursorAdapter is easier to use while the CursorAdapter requires more work but allows more customization.

SimpleAdapter

The SimpleAdapter links static data to views defined in a layout file. You specify the data as an ArrayList of Maps. Each entry in the ArrayList will display as a row in a list.

BaseAdapter

The BaseAdapter is a common base class for an Adapter that can be used in a ListView and a Spinner.

ListAdapter

The ListAdapter links the data and a ListView displaying the data. The List View can display any data type provided it’s wrapped in a ListAdapter.

ListView

A ListView is a ViewGroup that displays a list of scrollable items. Items are inserted in the list using an Adapter.

The Adapter gets the data from a source such as an array, converts each item into a view and places the view in the list.

Android Adapter Diagram ListView

A ListView displaying a list of items

GridView

A GridView is a ViewGroup that display items in a 2 dimensional scrollable grid. Here's a tutorial where we use a GridView to display a grid of images, GridView tutorial using the Picasso Library.

An Adapter gets the items from a data source, creates a view to enclose the item and then inserts the view in the parent grid.

Android Adapter Diagram GridView

Use an Adapter to populate a GridView

SpinnerAdapter

The SpinnerAdapter links the data to a Spinner.

The data can come from any source but must be provided by a SpinnerAdapter (for example an ArrayAdapter or a CursorAdapter).

Note that the BaseAdapter implements SpinnerAdapter. Both the CursorAdapter and the ArrayAdapter are subclasses of BaseAdpater so you can also use any of these to load your Spinner.

Android Adapter Diagram Spinner

Clicking on the Spinner displays a dropdown list of items to select from

I hope that you have found this tutorial helpful.

If you need more info on ArrayAdapters and lists, have a look at our Android ListView tutorial.

Also, have a look at Loading data using Android's Loaders where we use a SimpleCursorAdapter.