Using convertView in getView() to make ListView efficient

Posted: May 15, 2013 in Android Posts, Android Tutorials

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.

If the convertView is null, we have to run the code below to create a new View.
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);

If convertView is NOT null, we can simple re-use the convertView as the new View. It will happen when a new row appear and a old row in the other end roll out.

There are only seven elements in DayOfWeek[], it’s not enough to demo this case. So we modify to use month[] of 12 elements.

Using convertView in getView() to make ListView efficient

Note the change in the method getView(): changed from last exercise “Custom ArrayAdapter, with with different icons“.

package com.exercise.AndroidList;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidList extends ListActivity {

public class MyCustomAdapter extends ArrayAdapter<String> {

public MyCustomAdapter(Context context, int textViewResourceId,
  String[] objects) {
 super(context, textViewResourceId, objects);
 // TODO Auto-generated constructor stub

public View getView(int position, View convertView, ViewGroup parent) {
 // TODO Auto-generated method stub
 //return super.getView(position, convertView, parent);

 View row = convertView;

  LayoutInflater inflater=getLayoutInflater();
  row=inflater.inflate(R.layout.row, parent, false);

 TextView label=(TextView)row.findViewById(;
 ImageView icon=(ImageView)row.findViewById(;

 if (month[position]=="December"){

 return row;

String[] month = {
    "January", "February", "March", "April",
    "May", "June", "July", "August",
    "September", "October", "November", "December"

 /** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {
     /*setListAdapter(new ArrayAdapter<String>(this,
       R.layout.row,, DayOfWeek));*/
     setListAdapter(new MyCustomAdapter(AndroidList.this, R.layout.row, month));

protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s