Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkbox in CursorAdapter

i read this post

but i cant solve my problem . i using the CursorAdapter in listview.

i have a checkbox in the each item of list.if you checked the checkbox and the scrolling up and down . the checkbox will be disable. i cant fix it . please help me.

@Override
public void bindView(View view, Context context, final Cursor cursor) {

    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 likes = cursor.getString(cursor.getColumnIndex("like"));

    if (likes.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()) {
                ContentValues cv = new ContentValues();
                cv.put("like", "yes");
                mydb.update("list", cv, "id ="+cursor.getString(1), null);
            } else {
                ContentValues cv = new ContentValues();
                cv.put("like", "no");
                mydb.update("list", cv, "id ="+cursor.getString(1), null);
            }
            mydb.close();
        }
    });

    }

i using an external database and like column for saving the favorite item.

like image 891
hamedjj Avatar asked May 19 '14 16:05

hamedjj


1 Answers

if (likes.equals("yes")) {
    repeatChkBx.setChecked(true);
} else {
    repeatChkBx.setChecked(false);
}

Here you set CheckBox mark time and again when a bindView called after your scrolling of a ListView. A user choice does not memorized and reset in the CheckBox. You need to memorized user choice of the CheckBox and set it in bindView.

like image 51
DenisMath Avatar answered Oct 30 '22 17:10

DenisMath