i'm using CursorAdapter for reading database in listview. i have a checkbox in the each item of the list that when the checkbox was checked by user the favorite Column in my database change the yes and the item added to the favorite.
everything is ok and the favorite column changed but when i scroll up and down the list the checkbox going to unchecked. and if you restarting the app the checkbox have been checked
what should i do for this problem:
sorry for my bad english:
CursorAdapter class:
public class MyAdapter extends CursorAdapter {
Context b;
LayoutInflater inflater;
@SuppressWarnings("deprecation")
public MyAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
b= (Context) context;
}
@SuppressWarnings("unused")
@Override
public void bindView(View view, Context context, final Cursor cursor) {
// TODO Auto-generated method stub
TextView tv1 = (TextView)view.findViewById(R.id.txt_name);
TextView tv2 = (TextView)view.findViewById(R.id.txt_numer);
tv1.setText(cursor.getString(2));
tv2.setText(cursor.getString(3));
final int pos = cursor.getPosition();
final CheckBox repeatChkBx = (CheckBox)view.findViewById(R.id.favorite_check);
String me = cursor.getString(cursor.getColumnIndex("like"));
if (me.equals("yes")) {
repeatChkBx.setChecked(true);
} else {
repeatChkBx.setChecked(false);
}
repeatChkBx.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
MyDatabase MyDatabase = new MyDatabase(b);
SQLiteDatabase mydb = MyDatabase.getWritableDatabase();
cursor.moveToPosition(pos);
if (repeatChkBx.isChecked()) {
mydb.execSQL("update list set like = 'yes' where id = " + cursor.getString(1));
}else{
mydb.execSQL("update list set like = 'no' where id = " + cursor.getString(1));
}
}
});
}
protected Context getActivity() {
// TODO Auto-generated method stub
return null;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.item, parent, false);
}
}
screenshot: