0

コード:

SimpleCursorAdapter ada = new SimpleCursorAdapter(this,
                R.layout.custom_layout, ssCursor, new String[] {
                        "String" }, new int[] {
                        R.id.txt2 });
        lvSms.setAdapter(ada); 

  btnDel.setOnClickListener(new View.OnClickListener() {

            private ArrayList<String> msgArr;

            public void onClick(View v) {

                LinearLayout ly = (LinearLayout) findViewById(R.id.lv);
                ListView lvMsg = (ListView) ly.getChildAt(3);
                int listCount = lvSms.getCount();
                for (int a = 0 ; a < listCount ; a++)
                {

                    LinearLayout ll = (LinearLayout) lvMsg.getChildAt(a);
                    LinearLayout l2 = (LinearLayout) ll.getChildAt(0);
                    LinearLayout l3 = (LinearLayout) l2.getChildAt(0);
                    CheckBox chkBox =(CheckBox) l3.getChildAt(0);
                    boolean valueOfChkBox = chkBox.isChecked();
                    if (valueOfChkBox) {
                        LinearLayout l4 = (LinearLayout) l2.getChildAt(1);
                        TextView txt1 = (TextView) l4.getChildAt(0);
                        msgArr = new ArrayList<String>();
                        msgArr.add(txt1.getText().toString());
                        Log.i("hello", txt1.getText().toString());
                    }

                    } });

NullPointerException表示されている行を返すので、を取得してgetChildAtいます。チェックされているかどうかに関係なく、非表示の行もチェックする必要があります。では、どうすれば別のボタンで確認できますか?

4

2 に答える 2

0

LinearLayout ly = (LinearLayout) findViewById(R.id.lv);

上記の行-ルートレイアウトを取得しようとしていますか?その場合、は使用できませんfindViewById。むしろ使用:

getLayoutInflater().inflate(R.layout.mylayout)

getLayoutInflater()アクティビティのメソッドです

于 2012-05-06T17:57:21.927 に答える
0

TextViewでチェックされている行からsのテキストを取得したいと思いますListView。すべての行を使用して解析することはできませんgetChildAt()。代わりに、アダプターに渡したデータであるカーソルを使用する必要があります(そしてどの行がチェックされているかを把握します)。レイアウトにカスタム行を使用するため、 (カスタムアダプターを使用して)checked/unchecked行のステータスを何らかの方法で維持する必要があります。CheckBoxesテキストを取得するときは、それをクリックするButtonだけで、現在チェックされている行を見つけて、カーソルから必要なテキストを直接抽出します。

のステータスを保存する簡単な方法は、CheckBoxesどちらを実行ArrayList<Boolean>するかに応じて変更することCheckBoxです(おそらくそれほど効率的ではありません)。

private class CustomAdapter extends SimpleCursorAdapter {
            // this will hold the status of the CheckBoxes
        private ArrayList<Boolean> checkItems = new ArrayList<Boolean>();

        public CustomAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
            int count = c.getCount();
            for (int i = 0; i < count; i++) {
                checkItems.add(false);
            }

        }

            //this method returns the current status of the CheckBoxes
            //use this to see what CheckBoxes are currently checked
        public ArrayList<Boolean> getItemsThatAreChecked() {
            return checkItems;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);
            int position = cursor.getPosition();
            CheckBox ckb = (CheckBox) view.findViewById(R.id.checkBox);
            ckb.setTag(new Integer(position));
            ckb.setChecked(checkItems.get(position));
            ckb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    Integer realPosition = (Integer) buttonView.getTag();
                    checkItems.set(realPosition, isChecked);
                }

            });
        }

    }

次に、ArrayList取得したデータを解析getItemsThatAreChecked()し、カーソルからデータを抽出します。ここに小さな例がありますhttp://pastebin.com/tsZ6mzt9(またはここでgit://gist.github.com/2634525.gitとレイアウト)

于 2012-05-08T12:19:03.497 に答える