Android from Scratch – What are Adapters?

What is Adapter?


Adapters in Android is a link between the AdapterView and underlying data for that view. The Adapter provides the opportunity to use data items.

Here is What Developers Guide tells about Adapters

AdapterView Class:


The adapter view is a derived class of view group. Adapter view accesses the data through adapter object indirectly, Adapter view is also called as view group.

Responsibilities of  AdapterView


  • Handling the user selection
  • Filling the layout data through the adapter

Quick Link – Download Professional Android Topics

There are the some normally used Adapter in Android to execute the data in the UI Components.

  1. Base Adapter
  2. ArrayAdapter
  3. List Adapter
  4. Cursor Adapter
  5. Grid view

Now we discuss each Adapters one by one in detail:

Base Adapter


It is a  common base class for an adapter that can be used in a Listview and Spinner. If we have need ofcustomized list in a list view we able to create our own adapter and extend base adapter. To create a custom Adapter for  Showing a custom list item from extended by Base Adapter.

Custom Adapter extends the BaseAdapter code:

public class CustomAdapter extends BaseAdapter {

@Override

public int getCount() {

return 0;

}

ArrayAdapter


Array adapter is an adapter if ever had a list of single item which is backed by an array. For Example, List of Contacts or Names.

Here is How ArrayAdapter looks:

ArrayAdapter(Context context, int resources, int textViewResourcesId, T[] objects)

List Adapter


The ListAdapter has contained the data and a ListView that displaying the data. The List View can show any data type provided it’s wrapped in a ListAdapter.

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

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

CursorAdapter


A CursorAdapter is a Cursor’s data to a ListView. 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 much easier to use while the CursorAdapter, it needs better work but allows more customization.

GridView


A GridView is a ViewGroup that displays items in a 2-dimensional scrollable grid.

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