1

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

|文本视图复选框|

滚动复选框时,回收时会丢失状态。我曾尝试使用 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();
 });

}

我将拥有状态存储在数据库中,然后我想检查拥有状态以设置复选框。这样,当用户进入活动时,他们将看到他们之前选择的项目,以及任何新的选择。除了在屏幕外滚动视图会丢失新选择的项目的状态之外,这非常有效。有人可以给我一些关于如何保存状态的指示吗?

4

2 回答 2

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

        }
    }

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

于 2012-12-04T03:33:20.297 回答
0

将 OnCheckChangeListener 设置为 null 的一种简单方法。

您必须在适配器中添加此代码。

yourcheckboxid.setOnCheckChangeListener(null) 
于 2017-03-22T17:09:04.663 回答