I've scoured multiple questions on this and still have not found the way to implement what i'm looking for. I have a simple custom cursorAdapter that is in the form
|textview checkbox|
when scrolling the checkboxes lose state when recycled. I have tried using an arrayList to store the boolean values but when checking them, it does not seem to ever come back as true.
public class BrowseCardsAdapter extends CursorAdapter {
private LayoutInflater mInflater = null;
private Context mContext;
public DataBaseHelper myDbHelper = null;
public BrowseCardsAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);
mContext = context;
myDbHelper = new DataBaseHelper(mContext);
try {
myDbHelper.openDataBaseForUpdate();
} catch (SQLException sqle) {
throw sqle;
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.list_cards, parent, false);
}
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
final CheckBox cb_have = (CheckBox) view.findViewById(R.id.cb_have);
TextView cardNumber = (TextView) view.findViewById(R.id.tv_cardnumber);
final String cardNumberText = cursor.getString(cursor.getColumnIndex("CardNumber"));
final String have = cursor.getString(cursor.getColumnIndex("Have"));
cardNumber.setText(cardNumberText);
if (have.compareTo("true") == 0)
cb_have.setChecked(true);
else
cb_have.setChecked(false);
cb_have.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (cb_have.isChecked()) {
myDbHelper.addHaveToDatabase(cardNumberText, true);
cb_have.setChecked(true);
}
else {
myDbHelper.addHaveToDatabase(cardNumberText, false);
cb_have.setChecked(false);
}
myDbHelper.close();
});
}
i'm storing the have state in the database, then i want to check the have state to set the checkbox. this way, when the users comes into the activity they will see items they have previously selected, as well as any new selections. This works great except for the scenario of scrolling views offscreen that lose state of newly selected items. could someone give me some pointers on how to save the state?