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!
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.
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.
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 View
s, 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.
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.
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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With