2012-05-06 45 views
0

代码:NullPointerException异常而穿越一个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返回可见行,我也必须检查无形行,他们要么被选中或不。那么我怎么能在单独的按钮上检查它?

+0

你可以添加代码设置适配器(或者如果它是一个自定义适配器添加代码)? – Luksprog

+0

@Luksprog我已根据您的要求添加了适配器代码。请帮助... – kamil

+0

你想完成什么?一般来说不是代码方式。 – Barak

回答

0

我猜,你想从那些在ListView检查行获得TextView S的文本。您不能使用getChildAt()并仅解析所有行,而应使用您在适配器,光标(并确定要检查的行)中传递的数据。由于您使用自定义的行来布局,因此您必须以某种方式保持行CheckBoxes(使用自定义适配器)的checked/unchecked状态。何时可以获取文本,只需点击Button,然后只需找出当前选中的行,并从光标中直接提取所需的文本。

一个简单的方法来存储CheckBoxes的状态是有,你会改变,这取决于你CheckBox行为(可能不是有效的)的ArrayList<Boolean>

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与布局)

0

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

上面一行 - 它试图把根布局?如果是这样,你不能使用findViewById。而是使用:

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

getLayoutInflater()是活动的方法

+0

但我如何遍历列表视图项? – kamil