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.
Let’s assume you want to display a list in your Android app.
For this you will use the ListView
provided by Android.
ListView
s don’t actually contain any data themselves.
It’s just a UI element without data in it.
You can populate your ListView
s by using an Android adapter.
Adapter
is an interface whose implementations provide data and control the display of that data.
ListView
s 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);
TextView
to which the data is written