Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of `convertView` in ListView adapter?

Tags:

java

android

In android, I usually use MyAdapter extends ArrayAdapter to create view for the ListView, and as a result, I have to override the function

public View getView(int position, View convertView, ViewGroup parent) {
    // somecode here
}

However, i don't know exactly what convertView and parent do! Does anyone have a suggestion? More detail, more help! Thanks!

like image 355
Kingfisher Phuoc Avatar asked May 12 '12 02:05

Kingfisher Phuoc


People also ask

What is the purpose of the ViewHolder Design practice?

When you are developing an Android program; and you want to have a ArrayAdapter you can Simply have a Class (most of times with ViewHolder suffix) or directly inflate your convertView and find your view by id.

How do you connect an adapter with ListView write down the codes for it?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.


4 Answers

From the documentation,

convertView - The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.

In other words, this parameter is used strictly to increase the performance of your Adapter. When a ListView uses an Adapter to fill its rows with Views, the adapter populates each list item with a View object by calling getView() on each row. The Adapter uses the convertView as a way of recycling old View objects that are no longer being used. In this way, the ListView can send the Adapter old, "recycled" view objects that are no longer being displayed instead of instantiating an entirely new object each time the Adapter wants to display a new list item. This is the purpose of the convertView parameter.

like image 97
Alex Lockwood Avatar answered Oct 02 '22 03:10

Alex Lockwood


convertView is used to reuse old view.

Please understand Adapter functionality in android. Adapter enables you to reuse some view with new data.

So if a list is of 15 items, but window can show only 5 items, then at first convertView would be null, and we need to create new views for these five items, but when you scroll down, you have two options, either create 6-10 views, or re-use old views and load new data into these views. Adapter and convertView enables you to do the later method.

like image 36
jeet Avatar answered Oct 02 '22 04:10

jeet


convertView is the ListView Item Cache that is not visible, and hence it can be reused. It lets the ListView need not create a lot of ListItems, hence saving memeory and making the ListView more smooth.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if(convertView == null) {
        view = this.inflator.inflate(android.R.layout.simple_list_item_1, parent, false);
    }
    else {
        view = convertView;
    }
    // remaining implementation
    return view;
}
like image 35
Changwei Yao Avatar answered Oct 02 '22 04:10

Changwei Yao


Shorter Version :

Please read @Alex Lockwood's and @jeet's answer.

My Answer :

Before why, which is the better/proper way of using convertView in getView()? Well explained by Romain Guy in this video.

An example,

@Override
 public View getView(final int position, View convertView, ViewGroup parent)            {
   View rowView = convertView;
   ViewHolder holderObject;

 if (rowView == null) {
     rowView = inflater.inflate(R.layout.list_single_post_or_comment, parent, false);
     holderObject = new HolderForContent();
     mapHolder(holderObject, rowView);
     rowView.setTag(holderObject);
   } else {
     holderObject = (HolderForContent) convertView.getTag();
   }
   setHolderValues(holderObject, position);
   return rowView;
 }
 
 private class ViewHolder {
    TextView mTextView;
 }
 
 mapHolder(holderObject, rowView) {
    //assume R.id.mTextView exists
    holderObject.mTextView = rowView.findViewById(R.id.mTextView);
 }
 
 setHolderValues(holderObject, position) {
    //assume this arrayList exists
    String mString = arrayList.get(position).mTextViewContent;
    holderObject.mTextView.setText(mString);
 }

Above is just an example, you can follow any type of pattern. But remember this,

@Override
 public View getView(final int position, View convertView, ViewGroup parent) {
 
   if (convertView == null) {
        // todo : inflate rowView. Map view in xml.
   } else {
        // todo : get already defined view references
   }

   // todo : set data to display
 
 return rowView;
 
 }

Now coming to purpose of convertView. The why?

convertView is used for performance optimization [see chart in slide 14 by Romain Guy] by not recreating view that was created already.

Sources : Any corrections are welcome. I actually gathered this info through these links,

Read about getView() in Android developer documentation.

Romain Guy speaking about getView() in video "Turbo Charge Your UI" at Google IO 2009 and material used for presentation.

A wonderful blog by Lucas Rocha.

Those who want to take a deep dive into source code : ListView and a sample implementation of getView() can be seen in source code for arrayAdapter.

Similar SO posts.

what-is-convertview-parameter-in-arrayadapter-getview-method

how-do-i-choose-convertview-to-reuse

how-does-the-getview-method-work-when-creating-your-own-custom-adapter

like image 31
tpk Avatar answered Oct 02 '22 04:10

tpk