Home » Android » android – Holder Class in Adapter Class

android – Holder Class in Adapter Class

Posted by: admin June 15, 2020 Leave a comment


There is one simple question which after searching i wanted to ask that why we create static holder class and assign the views inside it?? Please clear my doubts it will be great help to me.

How to&Answers:

Your code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the "view holder" design pattern.

A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly. First, you need to create a class to hold your exact set of views.

You can read Android Guideline for more detail.

  1. So when you are using “View Holder” then you can easily access each view without the need for the look-up which saving valuable processor cycles.
  2. having a static inner class for the views greatly improves performance

you can also see Why does Android prefer static classes link.

One more interesting link How ListView Work, after reading this blog developer can clear logic of LisView and also why need to implement inner static class for listView.


The answer is also simple if you understand how an AdapterView works.

An AdapterView is a view whose children are determined by an Adapter. The AdapterView (more specifically the concrete implementaion like ListView) contains more information than what is shown at any given time. To optimize the memory consumption and also for the sake of performance, the Adapter usually reuses the Views representing the individual items. So you have less number of View objects than the corresponding data.

The reused objects can be a complex hierarchy of Views or ViewGroups. So, in case you want to find the individual View objects from within this hierarchy, you would need to depend on findViewById() method and this becomes costly when the view hierarchy has multiple levels. So for simplicity (and also to improve performance), the View-Holder pattern is used, where the individual View objects that we are interested in are assigned to a static inner class.

You can refer to Lars Vogel’s Android ListView and ListActivity – Tutorial for more information about the View-Holder pattern.


It’s an optimization point. Without a viewHolder, you’ll need to call the findViewById methods each time. with the viewHolder, you only need to call these once.

Example with a TextView and an ImageView:

  • Without ViewHolder:

    if (convertView == null) {
        convertView = mInflater.inflate(..., null);
    //Following called each time
    TextView tv = (TextView)convertView.findViewById(...);
    ImageView iv = (ImageView)convertView.findViewById(...);
  • With ViewHolder:

    if (convertView == null) {
        convertView = mInflater.inflate(..., null);
        holder = new ViewHolder();
        //called once
        holder.tv = (TextView) vi.findViewById(..);
        holder.iv = (ImageView) vi.findViewById(...);
    } else {
        holder = (ViewHolder) vi.getTag();