Hello android developers, I have read documentation on getView method of BaseAdapter and what I understood is view can be reused/recycled,so should check that this view is non-null and of an appropriate type before using. In my case every time convertview is null and new view is created. Though list is populated correctly,but I would like to know when view will be recycled and when it will create new view.
Asked
Active
Viewed 2,297 times
2 Answers
1
Basically it is recycled when you scroll your list. When item is hidden - it can be recycled and used as new visible item. Try to add ~100 items and scroll them and check how many views really created.

Mikita Belahlazau
- 15,326
- 2
- 38
- 43
-
hey nikita, thanx for answer. As per your suggestion I tried by adding many items. And as you said on scroll new items are being recycled.But I have a doubt on it, before scrolling,my first item is being recycled and other visible item are created. Why first item is created and recycled and then displayed. – Dory Feb 07 '13 at 07:36
-
@chitra I've tested it. And it showed me 23 items, all of them created. When I started scrolling - they recycled. Can you show example of your logs? – Mikita Belahlazau Feb 07 '13 at 07:51
-
`private class TextAdapter extends BaseAdapter{` `public int getCount() {` `return 100;` } `public View getView(int position, View convertView, ViewGroup parent) {` `TextView textview;` `if(convertView==null){` `textview = new TextView(context);` `textview.setText("new view " +position);` `Log.v("demo","created " + position);` `}else{` `textview =(TextView) convertView;` `textview.setText("recycled view " +position);` `Log.v("demo","recycled " + position);` `}` `return textview;` `}` `}` – Dory Feb 07 '13 at 09:12
-
i have post code over here. In log,For position 0 I am getting first log as "created 0" and then as "recycled 0". Am I going wrong somewhere..? – Dory Feb 07 '13 at 09:13
-
@chitra I think it's ok. It may be android weird behaviour. What version do use? Still it shouldn't make a big difference for your. – Mikita Belahlazau Feb 07 '13 at 09:30
-
hey nikita, I am using android 2.3 version – Dory Feb 07 '13 at 09:35
-
@chitra does it cause any problems? Or it is curiosity? – Mikita Belahlazau Feb 07 '13 at 10:42
0
so should check that this view is non-null and of an appropriate type before using
So, i believe you are using multiple Layouts in the ListView.
In my case every time convertview is null and new view is created
This might be because, you didn't use:
@Override
public int getItemViewType(int position) {
return dataArray[position].getType();
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
Though list is populated correctly,but I would like to know when view will be recycled and when it will create new view
It won't recycle automatically, you should check for the availability of ConvertView(not null) and use it, instead of inflating a new View.
This way:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE1:
//inflate type1
break;
case TYPE2:
//inflate type2
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
}
And ConvertView is available (not null) to be resused, when the View of that particular type is inflated at least once and is no longer visible, because you scrolled the List.

Archie.bpgc
- 23,812
- 38
- 150
- 226