自定义的CursorAdapter和CheckBox状态 [英] Custom CursorAdapter and CheckBox states

查看:295
本文介绍了自定义的CursorAdapter和CheckBox状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有尝试使用<一个问题href=\"http://stackoverflow.com/questions/9424023/is-this-custom-cursoradapter-for-a-listview-properly-$c$cd-for-android\">suggested CursorAdapter的执行情况,CursorLoader一起。
CursorAdapter的工作beatifully的时候,我可以为它提供通过游标一组静态的数据,但是当我试着用Cu​​rsorLoader结合这一点,我得到一个问题nullpointers。我已经固定下来的事实,当我喂适配器光标,它最初是空的(设置为null作为一个CursorLoader实现打交道时经常建议)。
该适配器上的实例化,循环通过游标找出一个复选框处于什么状态,然后经过填充各种textviews和窗口小部件的数据。不幸的是,光标在实例空,只有当CursorLoader做是为了被馈送的数据集。
我想弄清楚它是否在所有可能与CursorLoader配合使用的CursorAdapter和倒很AP preciate一些帮助。

下面是我的完整适配器:

 公共类ShopperListCursorAdapter扩展的CursorAdapter实现OnClickListener,LOG {
    私人LayoutInflater mInflater;
    私人GroceriesHelper mHelper;
    私人列表&LT;布尔&GT; mCheckedState;    公共ShopperListCursorAdapter(上下文的背景下,光标光标,GroceriesHelper帮手,INT标志){
        超(上下文,光标,标志);
        mHelper =帮手;
        mInflater = LayoutInflater.from(上下文);
        为(mCheckedState =新的ArrayList&所述;布尔&GT;();!cursor.isAfterLast(); cursor.moveToNext()){
            mCheckedState.add(cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_CHECKED))!= 0);
        }
    }    @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
        Log.d(TAGNewView的」);
        查看查看= mInflater.inflate(R.layout.listview_row,NULL);
        ViewHolder持有人=新ViewHolder();
        holder.amount =(TextView中)view.findViewById(R.id.text_amount);
        holder.unit =(TextView中)view.findViewById(R.id.text_unit);
        holder.item =(TextView中)view.findViewById(R.id.text_item);
        holder.checked =(复选框)view.findViewById(R.id.check_item);
        holder.checked.setOnClickListener(本);
        view.setTag(保持器);
        返回视图。
    }    @覆盖
    公共无效bindView(查看视图,上下文的背景下,光标光标){
        Log.d(TAGbindView);
        RowData数据=新RowData();
        data.id = cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_ID));
        data.amount = cursor.getString(cursor.getColumnIndex(Groceries.COLUMN_AMOUNT));
        data.unit =将String.valueOf(cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_UNIT_ID)));
        data.item = cursor.getString(cursor.getColumnIndex(Groceries.COLUMN_ITEM));
        data.position = cursor.getPosition();        ViewHolder支架=(ViewHolder)view.getTag();
        holder.amount.setText(data.amount);
        holder.unit.setText(data.unit);
        holder.item.setText(data.item);
        holder.checked.setChecked(mCheckedState.get(data.position));
        holder.checked.setTag(数据);
    }    @覆盖
    公共无效的onClick(查看视图){
        布尔知名度=((复选框)视图).isChecked();
        RowData数据=(RowData)view.getTag();
        Log.d(TAG,数据:+ data.position);
        mCheckedState.set(data.position,能见度);
        mHelper.setChecked(data.id,能见度==真?1:0);
    }    私有静态类ViewHolder {
        TextView的金额;
        TextView的单位;
        TextView的项目;
        选中复选框;
    }    私有静态类RowData {
        INT ID;
        串量;
        串单元;
        字符串项;
        INT位置;
    }
}


解决方案

复制和粘贴

 为(mCheckedState =新的ArrayList&LT;布尔&GT;(!); cursor.isAfterLast(); cursor.moveToNext()){
    mCheckedState.add(cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_CHECKED))!= 0);
}

到您的适配器类中的公共helper方法(即公共无效populateAdapter())。

然后,在 onLoadFinished 交换光标(用 mAdapter.swapCursor(光标)),然后调用 mAdapter.populateAdapter()。这将确保你的光标被绑定到适配器尝试填充 mCheckedState 之前。

这是说,我不是很确定你正在尝试在首位, mCheckedState 做...为什么你不能只选中/取消意见直接使用光标 bindView

I have a problem with trying to use a suggested CursorAdapter implementation together with a CursorLoader. The CursorAdapter works beatifully when I can provide it with a static set of data through a Cursor, but when I try and combine this with a CursorLoader I get a problem with nullpointers. I have pinned it down to the fact that when I feed the adapter a cursor, it is initially empty (set to null as is frequently suggested when dealing with a CursorLoader implementation). The Adapter, on instantiation, loops through the cursor to figure out what state a checkbox is in and then goes through to populate data in various textviews and widgets. Unfortunately, the cursor is null on instantiation, only to be fed sets of data when the CursorLoader is done. I'm trying to figure out if it's at all possible to use this CursorAdapter together with a CursorLoader and would really appreciate some help.

Here's my complete Adapter:

public class ShopperListCursorAdapter extends CursorAdapter implements OnClickListener, LOG {
    private LayoutInflater mInflater;
    private GroceriesHelper mHelper;
    private List<Boolean> mCheckedState;

    public ShopperListCursorAdapter(Context context, Cursor cursor, GroceriesHelper helper, int flags) {
        super(context, cursor, flags);
        mHelper = helper;
        mInflater = LayoutInflater.from(context);
        for(mCheckedState = new ArrayList<Boolean>(); !cursor.isAfterLast(); cursor.moveToNext()) {
            mCheckedState.add(cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_CHECKED)) != 0);
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        Log.d(TAG, "newView");
        View view = mInflater.inflate(R.layout.listview_row, null);
        ViewHolder holder = new ViewHolder();
        holder.amount = (TextView) view.findViewById(R.id.text_amount);
        holder.unit = (TextView) view.findViewById(R.id.text_unit);
        holder.item = (TextView) view.findViewById(R.id.text_item);
        holder.checked = (CheckBox) view.findViewById(R.id.check_item);
        holder.checked.setOnClickListener(this);
        view.setTag(holder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Log.d(TAG, "bindView");
        RowData data = new RowData();
        data.id = cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_ID));
        data.amount = cursor.getString(cursor.getColumnIndex(Groceries.COLUMN_AMOUNT));
        data.unit = String.valueOf(cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_UNIT_ID)));
        data.item = cursor.getString(cursor.getColumnIndex(Groceries.COLUMN_ITEM));
        data.position = cursor.getPosition();

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.amount.setText(data.amount);
        holder.unit.setText(data.unit);
        holder.item.setText(data.item);
        holder.checked.setChecked(mCheckedState.get(data.position));
        holder.checked.setTag(data);
    }

    @Override
    public void onClick(View view) {
        boolean visibility = ((CheckBox) view).isChecked();
        RowData data = (RowData) view.getTag();
        Log.d(TAG, "data: " + data.position);
        mCheckedState.set(data.position, visibility);
        mHelper.setChecked(data.id, visibility == true ? 1 : 0);
    }

    private static class ViewHolder {
        TextView amount;
        TextView unit;
        TextView item;
        CheckBox checked;
    }

    private static class RowData {
        int id;
        String amount;
        String unit;
        String item;
        int position;
    }
}

解决方案

Copy and paste

for(mCheckedState = new ArrayList<Boolean>(); !cursor.isAfterLast(); cursor.moveToNext()) {
    mCheckedState.add(cursor.getInt(cursor.getColumnIndex(Groceries.COLUMN_CHECKED)) != 0);
}

into a public helper method in your adapter class (i.e. public void populateAdapter()).

Then, in onLoadFinished swap the cursor in (with mAdapter.swapCursor(cursor)) and then call mAdapter.populateAdapter(). This will ensure that your Cursor is bound to the adapter before you attempt to populate mCheckedState.

That said, I'm not entirely sure what you are trying to do with mCheckedState in the first place... why can't you just check/uncheck the views directly using the Cursor in bindView?

这篇关于自定义的CursorAdapter和CheckBox状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆