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 f
You could also subclass ResourceCursorAdapter. In that case you only need to override the bindview method:
public class MySimpleCursorAdapter extends ResourceCursorAdapter {
public MySimpleCursorAdapter(Context context, Cursor c) {
super(context, R.layout.myLayout, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
if (viewHolder == null) {
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}
viewHolder.bind(cursor, context);
}
/**
* A ViewHolder keeps references to children views to avoid unnecessary calls
* to findViewById() on each row (especially during scrolling)
*/
private static class ViewHolder {
private TextView text;
private ToggleButton toggle;
public ViewHolder(View view) {
text = (TextView) view.findViewById(R.id.rowText);
toggle = (ToggleButton) view.findViewById(R.id.rowToggleButton);
}
/**
* Bind the data from the cursor to the proper views that are hold in
* this holder
* @param cursor
*/
public void bind(Cursor cursor, Context context) {
toggle.setChecked(0 != cursor.getInt(cursor.getColumnIndex("ENABLED")));
text.setText(cursor.getString(cursor.getColumnIndex("TEXT")));
}
}
}