Kingfisher Phuoc Kingfisher Phuoc - 1 month ago 5
Java Question

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

In android, I usually use

MyAdapter extends ArrayAdapter
to create view for the
, 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
do! Does anyone have a suggestion? More detail, more help! Thanks!


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.