Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the role of adapters in Android?

People also ask

Why do we use adapters?

An adapter is a physical device that allows one hardware or electronic interface to be adapted (accommodated without loss of function) to another hardware or electronic interface. In a computer, an adapter is often built into a card that can be inserted into a slot on the computer's motherboard.

What is adapter in Android and its types?

Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are ArrayAdapter,Base Adapter, CursorAdapter, SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter.

Is adapter an android?

In Android, whenever we want to bind some data which we get from any data source (e.g. ArrayList, HashMap, SQLite, etc.) with a UI component(e.g. ListView, GridView, etc.) then Adapter comes into the picture. Basically Adapter acts as a bridge between the UI component and data sources.

What is an adapter view in Android?

AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source.


Well adapters in Android are basically a bridge between the UI components and the data source that fill data into the UI Component

For example, Lists (UI Component) get populated by using a list adapter, from a data source array.


Let’s assume you want to display a list in your Android app. For this you will use the ListView provided by Android. ListViews don’t actually contain any data themselves. It’s just a UI element without data in it. You can populate your ListViews by using an Android adapter.

Adapter is an interface whose implementations provide data and control the display of that data. ListViews own adapters that completely control the ListView’s display. So adapters control the content displayed in the list as well as how to display it.

The Adapter interface includes various methods to communicate data to the ListView. You can create your own adapter from scratch by implementing BaseAdapter.

public class ArrayAdapter<T> extends BaseAdapter implements Filterable {

// One of the constructors
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
    init(context, resource, textViewResourceId, Arrays.asList(objects));
}

void manyMoreMethods(){} 

}

Lets define an adapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
   android.R.layout.simple_list_item_1, android.R.id.text1, values);
  • First parameter: Context
  • Second parameter: Layout for the row
  • Third parameter: ID of the TextView to which the data is written
  • Fourth parameter: The array of data

I would like to share my understanding.

It's an interface between the data source and your layout (most probably ListView).

An analogy

Let's take the example of a mobile charger, or rather a USB cable. The wire can be considered as the adapter, while the data source and layout can be understood as the socket (plug-in point) and USB port (charging point) respectively.

In the case of mobile charging, the source of the power may be different, e.g. charging from a power bank, a socket or a laptop. The same is the case for the adapters used in Android. The data source may be changed depending upon the application requirement.

In short, an adapter in Android carries the data from a source (e.g. ArrayList<>) and delivers it to a layout (.xml file).


Adapters in Android are a bridge between the adapter view (e.g. ListView) and the underlying data for that view. Imagine what the world would have been without adapters!

Examples

  • A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.

  • The ListAdapter defines the layout for individual rows of the list and provides data to the ListView via the setAdapter() method of ListView.

  • Android provides several standard adapters; the most important are ArrayAdapter and CursorAdapter.

  • ArrayAdapter can handle data based on arrays or lists.

  • SimpleCursorAdapter can handle database related data.

Adapters are basically used to deliver content. One adapter you probably have in every application is the CursorAdapter which enables you to deliver content given by a cursor from a database query. A ListView has nearly always some sort of Adapter.


An adapter acts as a bridge between an AdapterView and the underlying data for that view. The adapter provides access to the data items and is responsible for creating a view for each item in the data set.

Adapters are a smart way to connect a View with some kind of data source. Typically, your view would be a ListView and the data would come in form of a Cursor or Array. So adapters come as subclasses of CursorAdapter or ArrayAdapter.