convertView in Android listview

I am sure you must have come face to face with one of Android’s most common composite view – the ListView. And also at some point or the other, you must have felt the need to populate your List with some custom views using your own adapter which must have extended the BaseAdapter , instead of simply using a ListAdapter .

All’s fine till this point, you merrily extend your adapter with the BaseAdapter, implement the unimplemented methods necessary until you see the getView( ) method with three parameters if I remember correctly.

1
2
3
4
public View getView(int position, View convertView, ViewGroup parent) {
        //What-to-do here
        return null;
    }

Yep, it is three alright. You can comprehend what position is. Parent is the ListView being populated, obviously. But what in the green droid is convertView? Well, it is the single child element of the ListView that android is going to use to inflate your custom view layout (ya, the one you defined in xml… if you have note done that yet, better do it now!).

Now a word on how it all works – the concept behind convertView. You see, your ListView,which you are going to populate with your custom elements, needs to know how many of them will fit the device screen at a given time. This is the job for Android to carry out. According to the parameters like height, width of the element, which you have (I hope) defined in the xml file for that custom view, and the screen metrics (height and width of the screen), Android will calculate how many of the elements can fill into the screen at one time. Say, that number is 7. This means, when your list view is first initialized, 7 items (even the partially displayed ones count) are displayed, with 7 calls going to the getView method in your adapter.During these first 7 calls, the convertView parameter is null, since there is no existing container to inflate your views in. Hence you must inflate your views for the first time in the

1
2
3
if(convertView == null){
     //Inflate Views here from xml and no other task
}

block.

Now, what happens as you scroll from the 7th element to the 8th. Obviously, your screen can only display 7 elements at a time (as assumed earlier) so the 1st element will go out of view once the 8th element is brought up. Again a call to getView() will be made, but the difference this time is that convertView will not be null. Android would recycle the container used to inflate the first element since the first element is no longer required in memory. When the list is scrolled up again to the first element, again a getView call, but this time convertView won’t be null, as the 8th element’s container will be used. This cycle and re-cycle of containers is repeated as we scroll up and down the list with subsequent calls to getView().

Hence, one must be careful to assign values to different variables inside the custom element/view, outside the block where we check convertView to be null, as otherwise, all elements after the 7th element as in our case, shall start repeating !

A Simple List View with custom elements

There you have it… convertView : Decoded 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s