Using convertView

The getView() method receives, as one of its parameters, a View named, by convention, convertView. Sometimes convertView will be null. In those cases, you have to create a new row View from scratch (e.g., via inflation), just as we did before.

However, if convertView is not null, then it is actually one of your previously created Views. This will be the case primarily when the user scrolls the ListView—as new rows appear, Android will attempt to recycle the views of the rows that scrolled off the other end of the list, to save you having to rebuild them from scratch.

Assuming that each of your rows has the same basic structure, you can use findViewById() to get at the individual widgets that make up your row and change their contents, then return convertView from getView() rather than create a whole new row.

For example, here is the getView() implementation from last time, now optimized via convertView (from the FancyLists/Recycling project at http://apress. com/):

public class RecyclingDemo extends ListActivity { TextView selection;

String[] items={"lorem", "ipsum", "dolor", "sit", "amet",

"consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque", "augue", "purus"};

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); setListAdapter(new IconicAdapter(this));


public void onListItemClick(ListView parent, View v, int position, long id) { selection.setText(items[position]);

class IconicAdapter extends ArrayAdapter { Activity context;

IconicAdapter(Activity context) { super(context, R.layout.row, items);


public View getView(int position, View convertView, ViewGroup parent) { View row=convertView;

LayoutInflater inflater=context.getLayoutInflater();

row=inflater.inflate(R.layout.row, null);

TextView label=(TextView)row.findViewById(; label.setText(items[position]);

ImageView icon=(ImageView)row.findViewById(;

if (items[position].length()>4) { icon.setImageResource(R.drawable.delete);



Here we check to see if the convertView is null and, if so we then inflate our row—but if it is not null, we just reuse it. The work to fill in the contents (icon image, text) is the same in either case. The advantage is that if the convertView is not null, we avoid the potentially expensive inflation step.

This approach will not work in every case, though. For example, it may be that you have a ListView for which some rows will have one line of text and others will have two. In this case, recycling existing rows becomes tricky, as the layouts may differ significantly. For example, if the row we need to create a View for requires two lines of text, we cannot just use a View with one line of text as is. We either need to tinker with the innards of that View, or ignore it and inflate a new View.

Of course, there are ways to deal with this, such as making the second line of text visible or invisible depending on whether it is needed. And on a phone every millisecond of CPU time is precious, possibly for the user experience, but always for battery life—more CPU utilization means a more quickly drained battery.

That being said, particularly if you are a rookie to Android, focus on getting the functionality right first, then looking to optimize performance on a second pass through your code rather than getting lost in a sea of Views, trying to tackle it all in one shot.

0 0

Post a comment