CursorAdapter中的复选框

ham*_*djj 6 checkbox android listview android-cursoradapter

我读过这篇文章

但我无法解决我的问题.我在listview中使用CursorAdapter.

我在列表的每个项目中都有一个复选框.如果选中了复选框并向上和向下滚动.该复选框将被禁用.我无法解决它.请帮我.

@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();
        }
    });

    }
Run Code Online (Sandbox Code Playgroud)

我使用外部数据库和列来保存喜欢的项目.

Den*_*ath 2

if (likes.equals("yes")) {
    repeatChkBx.setChecked(true);
} else {
    repeatChkBx.setChecked(false);
}
Run Code Online (Sandbox Code Playgroud)

在这里,当您滚动 ListView 后调用 bindView 时,您会一次又一次地设置 CheckBox 标记。用户的选择不会被记忆并在复选框中重置。您需要记住用户对复选框的选择并将其设置在bindView中。