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, asgetChildAt返回可见行,我还必须检查不可见行,无论它们是否被选中。那么我怎么能在单独的按钮上检查它呢?

4

2 回答 2

0

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

上面的行 - 它是否尝试采用根布局?如果是这样,您不能使用findViewById. 而是使用:

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

getLayoutInflater()是Activity的方法

于 2012-05-06T17:57:21.927 回答
0

我猜你想TextView从在ListView. 您不能使用getChildAt()并且只解析所有行,而是应该使用您在适配器中传递的数据,游标(并找出检查了哪些行)。因为您使用自定义行进行布局,所以您必须以某种方式维护checked/unchecked行的状态CheckBoxes(使用自定义适配器)。何时获取文本,单击Button,然后只需找出当前检查的行并直接从光标中提取您想要的文本。

存储状态的一种简单方法CheckBoxesArrayList<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 回答