2012-05-06 2 views
0

코드 통과하는 동안 : getChildAt가 가시 행을 반환하고 나는 또한 눈에 보이지 않는 행을 확인해야 중 하나가 확인되지 않았거나 같은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을 얻고있다. 어떻게 분리 된 버튼으로 확인할 수 있습니까?

+0

코드를 추가 할 수 있습니까? (또는 사용자 정의 어댑터 인 경우 추가 할 수 있습니까? 그 코드)? – Luksprog

+0

@Luksprog 귀하의 요청에 따라 어댑터 코드를 추가했습니다. 친절하게 도와주세요 ... – kamil

+0

성취하려는 것은 무엇입니까? 일반적으로 코드화되지 않습니다. – Barak

답변

0

난 당신이 ListView에서 확인되는 행에서 TextView의 텍스트를 가져올 같은데요이다. getChildAt()을 사용할 수없고 모든 행을 구문 분석 할 수는 없으므로 어댑터에서 전달하는 데이터 (커서가 검사 된 행을 파악)를 사용해야합니다. 레이아웃에 대해 사용자 정의 행을 사용하기 때문에 행의 checked/unchecked 상태를 유지해야합니다 (사용자 정의 어댑터 사용) CheckBoxes. 텍스트를 가져올 시간이 언제입니까? Button을 클릭하면 현재 선택된 행을 찾고 커서로 직접 원하는 텍스트를 추출합니다.

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 당신이 얻을 구문 분석 :

CheckBoxes의 상태를 저장하는 간단한 방법은 당신이 (아마 효율적이지)의 역할을 CheckBox에 따라 수정 함 ArrayList<Boolean>을하는 것입니다 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