Android 游标适配器中的复选框
我读了这个 但是我不能解决我的问题。我在listview中使用游标适配器 我在列表的每个项目中都有一个复选框。如果你选中了复选框并上下滚动。该复选框将被禁用。 我修不好。请帮帮我Android 游标适配器中的复选框,android,listview,checkbox,android-cursoradapter,Android,Listview,Checkbox,Android Cursoradapter,我读了这个 但是我不能解决我的问题。我在listview中使用游标适配器 我在列表的每个项目中都有一个复选框。如果你选中了复选框并上下滚动。该复选框将被禁用。 我修不好。请帮帮我 @Override public void bindView(View view, Context context, final Cursor cursor) { TextView tv1 = (TextView)view.findViewById(R.id.txt_name); TextView t
@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();
}
});
}
我使用一个外部数据库和like列来保存喜爱的项目
if (likes.equals("yes")) {
repeatChkBx.setChecked(true);
} else {
repeatChkBx.setChecked(false);
}
在这里,您可以在滚动ListView后调用bindView时反复设置复选框标记。用户选择不会在复选框中记忆和重置。您需要记住用户对复选框的选择并在bindView中设置它。为什么不使用自定义适配器呢?这将是一个更简单、更容易的解决方案。他正在使用自定义适配器。自定义适配器和游标适配器之间有什么不同吗?自定义适配器可以解决这个问题吗?我想我必须使用阵列适配器…那么我如何才能记住用户BindView中的选项请使用setOnItemClickListener for CursorAdapter并在onItemClick(*,*,int position,*)回调中记住位置参数。您也可以通过adapter.getItem(position)获取此回调上的光标,并记住一些光标标识值,例如数据库表行id