What's the role of adapters in Android?
Asked Answered
M

10

134

I want to know when, where and how adapters are used in the context of Android.

The information from Android's developer documentation was insufficient for me and I'd like to get a more detailed analysis.

Mantellone answered 9/9, 2010 at 8:28 Comment(0)
C
36

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
Collected answered 9/8, 2015 at 9:6 Comment(0)
W
49

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.

Westerman answered 9/9, 2010 at 13:54 Comment(1)
This is the same information that I can get from documentation. But I want a detailed explanationMantellone
C
36

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
Collected answered 9/8, 2015 at 9:6 Comment(0)
A
30

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).

Assumption answered 25/10, 2016 at 14:20 Comment(1)
The best example so far :D Good job!Oddball
K
8

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.
Katiekatina answered 30/7, 2014 at 7:4 Comment(0)
S
4

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.

Sucy answered 9/9, 2010 at 10:13 Comment(2)
This is the same information that I can get from documentation. But I want a detailed explanationMantellone
@Robin: why don't you celebrate the 10 year anniversary of your question by "accepting" one of the replies. Personally: 1) I found your question valuable (that's how I wound up here), 2) I found many of these replies helpful (including this one) ... and 3) I don't have a clue what you mean by "a more detailed explanation". Perhaps that's because I'm more interested in the "why" (as well explained above).; I'm more interested in the "forest" (like WarrenFaith's response) than the "detailed trees" (whichI CAN get from the documentation). Please upvote and accept :)Traprock
T
4

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.

Thrum answered 7/1, 2013 at 6:26 Comment(0)
P
3

An adapter manages the data model and adapts it to the individual rows in the list view. It extends the BaseAdapter class.

Every line in the list view consists of a layout which can be as complex as you want. A typical line in a list view has an image on the left side and two text lines in the middle.

Providential answered 16/7, 2014 at 12:29 Comment(0)
I
0

Adapter is simply used to achieve the listview concept. Not only for displaying the list of data but also the some custom view. Suppose customer wants to use the list which has more number of textview (any other view) then , we have to use Adapter view in Android.

Infanticide answered 31/1, 2017 at 15:46 Comment(0)
P
0

There are already given multiple answer,But I want to give a different answer.

Adapter means you can that Its bridge provider.

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

Posner answered 19/9, 2017 at 7:35 Comment(0)
R
0

At the end, adapters are very useful to do a report. If one wants to show a report of some information, one can use this tool to show the data on the view.

Renfroe answered 28/5, 2018 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.