Android save Checkbox State in ListView with Cursor Adapter
Posted
by Ricardo
on Stack Overflow
See other posts from Stack Overflow
or by Ricardo
Published on 2010-03-09T05:48:31Z
Indexed on
2010/03/09
5:51 UTC
Read the original article
Hit count: 7613
I cant find a way to save the checkbox state when using a Cursor adapter. Everything else works fine but if i click on a checkbox it is repeated when it is recycled. Ive seen examples using array adapters but because of my lack of experience im finding it hard to translate it into using a cursor adapter. Could someone give me an example of how to go about it. Any help appreciated.
private class PostImageAdapter extends CursorAdapter {
private static final int s = 0;
private int layout;
Bitmap bm=null;
private String PostNumber;
TourDbAdapter mDbHelper;
public PostImageAdapter (Context context, int layout, Cursor c, String[] from, int[] to, String Postid) {
super(context, c);
this.layout = layout;
PostNumber = Postid;
mDbHelper = new TourDbAdapter(context);
mDbHelper.open();
}
@Override
public View newView(Context context, final Cursor c, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.image_post_row, null);
holder = new ViewHolder();
holder.Description = (TextView) row.findViewById(R.id.item_desc);
holder.cb = (CheckBox) row.findViewById(R.id.item_checkbox);
holder.DateTaken = (TextView) row.findViewById(R.id.item_date_taken);
holder.Photo = (ImageView) row.findViewById(R.id.item_thumb);
row.setTag(holder);
int DateCol = c.getColumnIndex(TourDbAdapter.KEY_DATE);
String Date = c.getString(DateCol);
int DescCol = c.getColumnIndex(TourDbAdapter.KEY_CAPTION);
String Description = c.getString(DescCol);
int FileNameCol = c.getColumnIndex(TourDbAdapter.KEY_FILENAME);
final String FileName = c.getString(FileNameCol);
int PostRowCol = c.getColumnIndex(TourDbAdapter.KEY_Post_ID); String RowID = c.getString(PostRowCol);
String Path = "sdcard/Tourabout/Thumbs/" + FileName + ".jpg";
Bitmap bm = BitmapFactory.decodeFile(Path, null);
holder.Photo.setImageBitmap(bm);
holder.DateTaken.setText(Date);
holder.Description.setText(Description);
holder.cb.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) { CheckBox cBox = (CheckBox) v; if (cBox.isChecked()) {
mDbHelper.UpdatePostImage(FileName, PostNumber);
}
else if (!cBox.isChecked()) {
mDbHelper.UpdatePostImage(FileName, "");
}
} }); return row;
};
@Override
public void bindView(View row, Context context, final Cursor c) {
ViewHolder holder;
holder = (ViewHolder) row.getTag();
int DateCol = c.getColumnIndex(TourDbAdapter.KEY_DATE);
String Date = c.getString(DateCol);
int DescCol = c.getColumnIndex(TourDbAdapter.KEY_CAPTION);
String Description = c.getString(DescCol);
int FileNameCol = c.getColumnIndex(TourDbAdapter.KEY_FILENAME);
final String FileName = c.getString(FileNameCol);
int PostRowCol = c.getColumnIndex(TourDbAdapter.KEY_Post_ID);
String RowID = c.getString(PostRowCol);
String Path = "sdcard/Tourabout/Thumbs/" + FileName + ".jpg";
Bitmap bm = BitmapFactory.decodeFile(Path, null);
File x = null;
holder.Photo.setImageBitmap(bm);
holder.DateTaken.setText(Date);
holder.Description.setText(Description);
holder.cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cBox = (CheckBox) v;
if (cBox.isChecked()) {
mDbHelper.UpdatePostImage(FileName, PostNumber);
}
else if (!cBox.isChecked()) {
mDbHelper.UpdatePostImage(FileName, "");
}
}
});
}
}
static class ViewHolder{ TextView Description; ImageView Photo; CheckBox cb; TextView DateTaken;
}
}
© Stack Overflow or respective owner