遍历ListView时出现NullPointerException

时间:2012-05-06 17:52:28

标签: android android-listview

代码:

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返回可见的行,我还要检查不可见的行,要么检查它们,要么不检查。那我怎么能在单独的按钮上查看呢?

2 个答案:

答案 0 :(得分:0)

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

以上一行 - 是否尝试采用根布局?如果是这样,则无法使用findViewById。而是使用:

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

getLayoutInflater()是Activity的方法

答案 1 :(得分:0)

我猜你想要从TextView中检查的行中获取ListView的文本。您不能使用getChildAt()并只解析所有行,而应使用您在适配器中传递的数据,光标(并确定检查哪些行)。因为您使用自定义行进行布局,所以您必须以某种方式维护行checked/unchecked的{​​{1}}状态(使用自定义适配器)。什么时候获取文本,只需单击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并从光标中提取数据。这里有一个小例子http://pastebin.com/tsZ6mzt9(或者这里有git://gist.github.com/2634525.git和布局)