在textView中设置Paint.STRIKE_THRU_TEXT_FLAG

时间:2012-12-01 09:18:49

标签: android listview

我遇到的问题是,当用户点击checkbox内的listView时,textView将不会向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;
        }

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

放在哪里
listName.setPaintFlags(listName.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);

1 个答案:

答案 0 :(得分:1)

我想我可能知道你现在可能会尝试什么,看看以下是否适合你:

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方法返回数组,以便在活动中进行数据库操作。