Monday, June 14, 2010

Using convertView in getView() to make ListView efficient

Refer to document of Android Developers about getView(); it's stated that :

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():

AndroidList.java changed from last exercise "Custom ArrayAdapter, with with different icons".
package com.exercise.AndroidList;

import android.app.ListActivity;
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
}

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

View row = convertView;

if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}

TextView label=(TextView)row.findViewById(R.id.weekofday);
label.setText(month[position]);
ImageView icon=(ImageView)row.findViewById(R.id.icon);

if (month[position]=="December"){
icon.setImageResource(R.drawable.icon);
}
else{
icon.setImageResource(R.drawable.icongray);
}

return row;
}
}

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

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

@Override
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();
}

}

9 comments:

  1. its the best tutorial for ListView.

    ReplyDelete
  2. Hello,

    I failed to run this tutorial hence I am unable to successfully design what needs to be there in the .xml file under layout. Can you please help me.

    Thank you very much

    ReplyDelete
  3. hello One Comeros,

    Please refer to the exercise of Custom ArrayAdapter, with different icons. You can get a example.

    ReplyDelete
  4. How about tutorial when click the list going to another ListView

    ReplyDelete
  5. Thx for the tutorial Android Er

    ReplyDelete
  6. Thank you for this example, help me a lot with my project, was very clean and fast to understand!

    ReplyDelete
  7. This article is very short and easy to understand.

    ReplyDelete