checkbox in CursorAdapter
Asked Answered
R

1

6

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.

Rhombohedron answered 19/5, 2014 at 16:5 Comment(5)
why dont you use custom adapter it will be a much simpler and easy solutionMole
he is using a custom adapterLipski
is there any different between CustomAdapter and CursorAdapter?Rhombohedron
CustomAdapter can solve this problem?Rhombohedron
i think i have to us arrayadapter ....Rhombohedron
U
2
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.

Uncurl answered 19/5, 2014 at 17:4 Comment(3)
so how can i memorized user choice in the bindViewRhombohedron
Please use setOnItemClickListener for CursorAdapter and memorize position argument in the onItemClick(*, *, int position, *) callback.Uncurl
You can also get Cursor on this callback by adapter.getItem(position) and memorize some Cursor identity value, for example database table row _idUncurl

© 2022 - 2024 — McMap. All rights reserved.