5

ユーザーが の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;
        }

チェックボックスは正常に動作します。しかし問題は、ユーザーがチェックボックスをクリックすると、取り消し線が表示されないことです。誰がどこに置くべきか知っています

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 に答える