Use of Adapter is a very common practice in Android development. As per the standard definition,
An Adapter object acts as a bridge between an
AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.
In a layman term, we use an adapter to place data from one place to another place. Here in the definition above, “Adapter View” can be considered as the source and the “underlying data” as a destination. We generally use an adapter for the dynamic data. For example, let’s say we have a list view of tcountry name. As we all know, a list view is nothing but a collection of items(or rows). Where the structure of each row remains same. When we use an adapter, we create just one item’s structure and pass this structure to list view. All we need to do is to design just one ROW of a list view. Depending upon that row, we decide which kind of adapter to be used. We need to create a new XML file for the designing of our row (or the item of a list view). Also, It’s a good practice if we keep adapter operations in a different class(or .java file).
There are so many types of available in android, like...
-
Array Adapter<T>
-
ResourceCursorAdapter
-
ThemedSpinnerAdapter
Array Adapter and Custom Adapter are the most used Adapter(at least in my case). We can use Array Adapter if we have only one item. It could be of any type like <String>,<Integers> etc., While we use custom Adapter if we have more than one item.
Let’s say we have to show the list of user name in a list view(only Name) which Adapter should we use ?
----> ArrayAdapter
Now, what if we also need to display the profile pic along with the name of the users ?
----> Use CustomAdapter
The layout which we create (i.e., a separate XML file also called AdapterView), defines the content of the rows for any list view. The ListView waits for its row in another XML file. And then the role of Adapter comes into play. We write Adapter.java(Obviously, you can name it anything) file which acts as an interface between these two XML files. Means, through our code we change the data for each row of the list view.
It took a great amount of time for me to clear this Adapter concept. I thought of sharing my understanding with you all. Please comment below for any further clarity or modifications required to this post.
No comments:
Post a Comment