Adapter and Adapter - View
Adapter and Adapter - View
Adapter and Adapter View are so popular, that every time you see
any app with a List of items or Grid of items, you can say for sure
that it is using Adapter and Adapter View.
But what if the data is a set of 1 million products. Then using a loop
will not only consume a lot of time, making the app slow, also it
might end up eating all the runtime memory.
All these problems are solved by using Adapter and Adapter View.
Adapter View, is a View object, and can be used just like we use any
other interface widget. The only catch here is, that it needs
an Adapter to provide content to it as it is incapable of displaying
data on its own.
What is an Adapter?
An adapter acts like a bridge between a data source and the user
interface. It reads data from various data sources, coverts it into
View objects and provide it to the linked Adapter view to create UI
components.
The data source or dataset can be an Array object, a List object etc.
Suppose you have a dataset, like a String array with the following
contents.
Copy
Now, what does an Adapter do is that it takes the data from this
array and creates a View from this data and then, it gives
this View to an AdapterView. The AdapterView then displays the
data in the way you want.
Note: Adapter is only responsible for taking the data from a data
source and converting it into View and then passing it to the
AdapterView. Thus, it is used to manage the data. AdapterView is
responsible for displaying the data.
Therefore, you can take the data from a database or an ArrayList or
any other data source and then, you can display that data in any
arrangement. You can display it vertically (ListView), or in rows and
columns (GridView), or in drop-down menu (Spinners), etc.
GridView