Signup/Sign In

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.

Generally, when we create any List or Grid of data, we think we can use a loop to iterate over the data and then set the data to create the list or grid.

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.

Analogy for Adapter and Adapter View in Android

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.

You can create your own Adapter class by extending the BaseAdapter class, which is the parent class for all other adapter class. Android SDK also provides some ready-to-use adapter classes, such as ArrayAdapter, SimpleAdapter etc.


What is an Adapter View?

An Adapter View can be used to display large sets of data efficiently in form of List or Grid etc, provided to it by an Adapter.

When we say efficiently, what do we mean?

An Adapter View is capable of displaying millions of items on the User Interface, while keeping the memory and CPU usage very low and without any noticeable lag. Different Adapters follow different strategies for this, but the default Adapter provided in Android SDK follow the following tricks:

  1. It only renders those View objects which are currently on-screen or are about to some on-screen. Hence no matter how big your data set is, the Adapter View will always load only 5 or 6 or maybe 7 items at once, depending upon the display size. Hence saving memory.
  2. It also reuses the already created layout to populate data items as the user scrolls, hence saving the CPU usage.

Suppose you have a dataset, like a String array with the following contents.

String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

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.

There are different types of AdapterViews. Let's have a look at some of them:


ListView

ListView AdapterView in Android

It displays a vertically-scrollable collection of views, where each view is positioned immediately below the previous view in the list.


GridView

GridView AdapterView in Android

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.


Spinner

Spinners provide a quick way to select one value from a set of values. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

Spinner AdapterView in Android

Every AdapterView uses some approach for using the Adapter. We will discuss this approach in the respective AdapterView tutorials.