gpt4 book ai didi

android - 滚动后如何保持android复选框状态

转载 作者:行者123 更新时间:2023-11-30 03:53:36 26 4
gpt4 key购买 nike

我已经搜索了多个关于此的问题,但仍然没有找到实现我正在寻找的方法的方法。我有一个简单的自定义 cursorAdapter,形式为

| TextView 复选框|

当滚动时,复选框在回收时失去状态。我曾尝试使用 arrayList 来存储 bool 值,但在检查它们时,它似乎永远不会返回 true。

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

我将 have 状态存储在数据库中,然后我想检查 have 状态以设置复选框。这样,当用户进入 Activity 时,他们将看到他们之前选择的项目,以及任何新的选择。这很好用,除了屏幕外 ScrollView 丢失新选择项目状态的情况。有人可以给我一些关于如何保存状态的指示吗?

最佳答案

尝试返回使用集合。像这样以增量方式写入数据库是不明智的。

这里:

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

}
}

提供了两种填充集合的方法。至于您的技术,因为您不断地写入数据库,所以您无法使用制作适配器时使用的旧游标获取您的值。您必须在每次加载行时重新实例化游标。这些要求肯定会让您放弃。

关于android - 滚动后如何保持android复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13695193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com