Adapter in Android

Vaishnavi Barge
GDSC DYPCOE
Published in
2 min readDec 14, 2022

In Android, adapter acts like a bridge between UI component and data source that helps to fill data in the UI component.

It holds the data and sends the data to an adapter view which in return can show the data on different views like ListView,GridView,SpinnerView,etc.

The adapter is a design pattern. It adapts the data and transforms it into something the client can use. Let us see the example about RecyclerView, and how the data is adapted to it.

When you run the app, RecyclerView uses the adapter to figure out how to display your data on screen. RecyclerView asks the adapter to create a new list item view for the first data item in your list. Once it has the view, it asks the adapter to provide the data to draw the item. This process repeats until the RecyclerView doesn’t need any more views to fill the screen. If only 3 list item views fit on the screen at once, the RecyclerView only asks the adapter to prepare those 3 list item views (instead of all 10 list item views).

The adapter in the app contains several components. A layout is first made for each item on the list. The next step is to create a context-object instance and an ItemAdapter-class that accepts a list of input data items.
In the ItemAdapter that follows, an ItemViewHolder is created. It may be reused and represents a single list item view in the RecyclerView. The view holder is in responsible of updating and presenting data.
Three methods, onCreateViewHolder, getItemCount, and onBindViewHolder, must be implemented by the adapter.

If there are no existing view holders that it may reuse, the layout manager calls onCreateViewHolder to generate new view holders for the RecyclerView. As its name implies, getItemCount is used to fetch the item count for list data. When a list item view’s contents need to be changed, the layout manager uses onBindViewHolder. There is still one more step: RecyclerView must be made aware of the adapter once it has been created. This can be done in MainActivity of the course project. Precisely, in that activity’s onCreate-method. getItemCount, onBindViewHolder, and nCreateViewHolder.

--

--