5

我有一个问题,当用户点击checkbox里面时listViewtextView它不会收费Paint.STRIKE_THRU_TEXT_FLAG)。当用户单击 时checkbox,打击线将显示在 中textView

           public void bindView(View row, Context context, Cursor c) {
            // TODO Auto-generated method stub

            listName = (TextView) row.findViewById(R.id.produtName);
            listCheck = (CheckBox) row.findViewById(R.id.check);
                            Item tag = (Item) listCheck.getTag();
            String pos = helper.getProductId(c);
            Log.i(CN, "getView: no tag on " + pos);
            tag = new Item();
            tag.id = Integer.parseInt(pos);
            listCheck.setTag(tag);
            listCheck.setChecked(false);
                        String status = helper.getProductStatusT(c);
            if (Integer.parseInt(status) == 0) {
                listCheck.setChecked(true);
                listName.setPaintFlags(listName.getPaintFlags()
                        | Paint.STRIKE_THRU_TEXT_FLAG);
            } else {
                listCheck.setChecked(false);
                listName.setPaintFlags(listName.getPaintFlags()
                        & (~Paint.STRIKE_THRU_TEXT_FLAG));
            }


           public View getView(int position, View convertView, ViewGroup parent) {
            View tmpView = super.getView(position, convertView, parent);
            Log.i(CN, "getView:" + position);
            final CheckBox cBox = (CheckBox) tmpView.findViewById(R.id.check);
            Item tag = (Item) cBox.getTag();

            cBox.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Item tag = (Item) v.getTag();
                    if (tag == null)
                        Log.i(CN, "checkbox clicked no tag");

                    else

                        helper.updateStatus(tag.id);

                    Log.i(CN, "checkbox clicked tag=" + tag.id);

                 }
                    if (cBox.isChecked()) {
                        Log.i(CN, " Checked!");
                        // do some operations here
                    } else {
                        Log.i(CN, "NOT Checked!");
                        // do some operations here
                        helper.updateStatus2(tag.id);
                    }
                }
            });
            return tmpView;
        }

复选框工作正常。但问题是当用户点击复选框时,罢工线不会显示出来。任何人都知道我应该把

listName.setPaintFlags(listName.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
4

1 回答 1

2

我想我可能知道您现在可能正在尝试什么,看看以下是否对您有用:

public class CustomCursorAdapter extends CursorAdapter {

    private LayoutInflater inflator;
    private boolean[] arrCb;

    public CustomCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        arrCb = new boolean[c.getCount()];
        // ^ this will hold the checkbox states
        resetArrcb();
    }

    @Override
    public View newView(Context context, Cursor c, ViewGroup parent) {
        View v = inflator.inflate(R.layout.lv_row, parent, false);
        // inflate the xml that the textView and checkbox is in
        return v;
    }

    @Override
    public void bindView(View row, Context context, final Cursor cursor) {
        // you do everything else here. there's no need for getView when you use newView and bindView
        final TextView listName = (TextView) row.findViewById(R.id.produtName);
        final CheckBox listCheck = (CheckBox) row.findViewById(R.id.check);

        if (arrCb[cursor.getPosition()]) {
            listCheck.setChecked(true);
            listName.setPaintFlags(listName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        } else {
            listCheck.setChecked(false);
            listName.setPaintFlags(listName.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        }

        listCheck.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listCheck.isChecked()) {
                    arrCb[cursor.getPosition()] = true;
                } else {
                    arrCb[cursor.getPosition()] = false;
                }
                notifyDataSetChanged();
            }
        });

    }

    private void resetArrcb() {
        // i'm using this to fill the collection but you could easily loop through your 
        // database to fill it with the correct values.
        for (int i = 0; i < arrCb.length; i++) {
            arrCb[i] = false;
        }
    }

}

然后我不确定,但是您似乎想根据所选内容调整数据库。我建议在一切都结束时,当你的活动被退出时这样做。也许设置一个新方法,它只会遍历复选框集合,然后在数据库中执行您想要的操作。它应该与表行匹配 1 到 1。

编辑

更好的是,创建一个 getter 方法来返回您的数组,这样您就可以在您的活动中直接执行数据库操作。

于 2012-12-01T10:44:46.117 回答