I've been scouring throug the examples and tutorials but I can't seem to get my head around how to handle recycling within a subclassed SimpleCursorAdapter. I know that for regular ArrayAdapters you can check convertView for null and inflate if null from the xml and if not null, recycle, but I'm having a little trouble visualizing how that works with the from and to arrays within the SimpleCursorAdapter subclass. I tried to figure this out from the The Busy Coders Guide to Android Development by Commonsware but was unsuccessful. If anyone knows of any tips, examples, or tutorials, I would be grateful to see them.
I was unable to find any good examples for Subclassing the SimpleCursorAdapter to use the convertView recycle methodolgy so I ended up subclassing CursorAdapter instead. This actually worked quite well for my implementation and is blazing fast with a dramatic decrease in memory usage. Recycling views really works and I recommend it highly!
Here is an example of my implementation:
private static class MyNiftyAdapter extends CursorAdapter
{
private LayoutInflater mInflater;
private Cursor cur;
public MyNiftyAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
{
super(context, c, autoRequery);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if(convertView == null)
{
convertView = this.mInflater.inflate(R.layout.chamber_item, null);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.Name);
viewHolder.city = (TextView)convertView.findViewById(R.id.city);
viewHolder.state = (TextView)convertView.findViewById(R.id.state);
viewHolder.country = (TextView)convertView.findViewById(R.id.country);
convertView.setTag(viewHolder);
}else
{
viewHolder = (ViewHolder)convertView.getTag();
}
this.cur.moveToPosition(position);
viewHolder.name.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.NAME)));
viewHolder.city.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.CITY)));
viewHolder.state.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.STATE)));
viewHolder.country.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.COUNTRY)));
return convertView;
}
/* (non-Javadoc)
* @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Dont need to do anything here
}
/* (non-Javadoc)
* @see android.widget.CursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Dont need to do anything here either
return null;
}
static class ViewHolder
{
TextView name;
TextView city;
TextView state;
TextView country;
}
}
Each row in my list now displays name, city, state, and country exactly as I wanted. Hope this helps.
CursorAdapter does the work partially for you. You only need to override newView() when a new view needs to be created, and bindView() when an existing view is recycled.
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