2012-05-06 53 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