我已经在这方面搜索了多个问题,但仍然没有找到实现我正在寻找的方法。我有一个简单的自定义cursorAdapter,其格式为
| textview复选框|
滚动复选框时,在回收时会丢失状态。我已经尝试使用arrayList来存储布尔值,但是在检查它们时,它似乎永远不会回归真实。
public class BrowseCardsAdapter extends CursorAdapter {
private LayoutInflater mInflater = null;
private Context mContext;
public DataBaseHelper myDbHelper = null;
public BrowseCardsAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);
mContext = context;
myDbHelper = new DataBaseHelper(mContext);
try {
myDbHelper.openDataBaseForUpdate();
} catch (SQLException sqle) {
throw sqle;
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.list_cards, parent, false);
}
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
final CheckBox cb_have = (CheckBox) view.findViewById(R.id.cb_have);
TextView cardNumber = (TextView) view.findViewById(R.id.tv_cardnumber);
final String cardNumberText = cursor.getString(cursor.getColumnIndex("CardNumber"));
final String have = cursor.getString(cursor.getColumnIndex("Have"));
cardNumber.setText(cardNumberText);
if (have.compareTo("true") == 0)
cb_have.setChecked(true);
else
cb_have.setChecked(false);
cb_have.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (cb_have.isChecked()) {
myDbHelper.addHaveToDatabase(cardNumberText, true);
cb_have.setChecked(true);
}
else {
myDbHelper.addHaveToDatabase(cardNumberText, false);
cb_have.setChecked(false);
}
myDbHelper.close();
});
}
我将has状态存储在数据库中,然后我想检查有状态来设置复选框。这样,当用户进入活动时,他们将看到他们之前选择的项目,以及任何新的选择。除了在屏幕外滚动丢失新选项的状态之外,这种方法效果很好。有人可以给我一些关于如何拯救国家的指示吗?
答案 0 :(得分:1)
尝试回到使用集合。以这样的增量写入数据库是不明智的。
下面:
public class BrowseCardsAdapter extends CursorAdapter {
private LayoutInflater mInflater = null;
private boolean[] arrCb;
public BrowseCardsAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);
arrCb = new boolean[cursor.getCount()];
for (int i = 0; i < arrCb.length; i++) {
arrCb[i] = false;
}
// ^otherwise you can loop through the database (in your activity)
// to fill this array with the correct values and pass it as a parameter
// it'd look like initCb() below;
}
/* private boolean[] initCb() {
Cursor cursor = myDbHelper.query(query statement);
boolean[] arr = new boolean[cursor.getCount()];
int i = 0;
while (cursor.moveToNext()) {
arr[i] = cursor.getString(cursor.getColumnIndex("Have"));
// ^obviously you'd need to convert your value to boolean however
i++;
}
return arr;
}*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.list_cards, parent, false);
}
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
final CheckBox cb_have = (CheckBox) view.findViewById(R.id.cb_have);
TextView cardNumber = (TextView) view.findViewById(R.id.tv_cardnumber);
final String cardNumberText = cursor.getString(cursor.getColumnIndex("CardNumber"));
// final String have = cursor.getString(cursor.getColumnIndex("Have"));
cardNumber.setText(cardNumberText);
cb_have.setChecked(arrCb[cursor.getPosition()]);
cb_have.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (cb_have.isChecked()) {
arrCb[cursor.getPosition()] = true;
} else {
arrCb[cursor.getPosition()] = false;
}
}
});
}
}
提供两种填充集合的方法。至于你的技术,因为你经常写入数据库,你不能使用适配器所用的旧光标来获取你的值。您必须在每次加载行时重新实例化游标。当然,这些要求会让你放弃。
答案 1 :(得分:0)
将OnCheckChangeListener设置为null的一种简单方法。
您必须在适配器中添加此代码。
yourcheckboxid.setOnCheckChangeListener(null)