2013-07-29 49 views
0

我有一些複選框的列表視圖,我需要根據存儲在數組中的一些數據自動檢查它們。擴展底座適配器我的自定義適配器:getChildAt的Nullpointerexception

public class MyAdapter extends BaseAdapter 
    { 
     private Context context; 

     public SPCMjereAdapter(Context c) 
     {   
      context = c;      
     } 

     public int getCount() { 
      return MyArrList.size(); 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public int getlistItemsCount() 
     { 
      return listView.getChildCount(); 
     } 

     public View getView(final int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.activity_list_row, null); 

      } 
      // ColID 
      TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
      txtOpis.setText(MyArrList.get(position).get("OpisMjere") +"."); 

      // ColCode 
      TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode); 
      txtRbMjere.setText(MyArrList.get(position).get("RbMjere")); 


      // ColChk    
      CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); 
      Chk.setTag(MyArrList.get(position).get("RbMjere")); 


      return convertView; 

     } 

    } 

這也是我如何檢查項目

int k=0; 
    int j=0; 
    for (j=0; j<numberOfItems; j++) 
    { 

     LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout 
     CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk); 

     for (k=0; k<rbmjere.size(); k++) 
     { 
      if (checkbox.getTag().toString() == rbmjere.get(k).toString()) 
      { 
       checkbox.setChecked(true); 
      } 
     } 
    } 

的問題是線

LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); 

所以,如果我把這個代碼檢查項目的問題是,列表視圖顯示例如3項,但代碼只識別2項和第三項失蹤。如何檢測所有項目何時可見或如何檢測列表視圖的呈現完成時?

+3

我建議你在for循環中使用'int count = listView.getChildCount();'然後'for(j = 0; j user370305

+1

看看你的'for'循環 - 它只循環兩次 - 對於0和1(它<2,所以不包括2):) – g00dy

+0

但是在哪裏放?我想在listview加載所有數據後檢查項目。我不確定我的邏輯是否正確... – Josef

回答

0

問題解決了。裏面getView()我寫了這幾行,它只是我想要的作品:

for (k=0; k<rbmjere.size(); k++) 
      { 
       if (Chk.getTag().toString().equals(rbmjere.get(k).toString())) 
       { 
        Chk.setChecked(true); 

       } 
      } 

感謝所有的幫助和想法。

0

嘗試使用數組適配器代替鹼適配器或這樣的事情:

public class ArrayListAdapter<T> extends ArrayAdapter<T> { 



    private List<T> arrayList; 

     public ArrayListAdapter(Context context, int layout, List<T> arrayList) { 
      super(context, layout); 
      this.arrayList = arrayList; 
     } 

     @Override 
     public T getItem(int position) { 
      return arrayList.get(position); 
     } 

     @Override 
     public int getCount() { 
      return arrayList.size(); 
     } 
    } 

創建適配器的實例,並設置listView.setAdapter(arrayAdapter); 我看你不提供清單適配器其propably爲什麼你NullPointerException異常

+1

BaseAdapter可以與列表一起使用,issu else是 –

0

1.Maybe適配器沒有設置列表視圖但

Handler handler = new Handler();   
    handler.postDelayed(new Runnable() {    
     public void run() { 
       //your code 
       CheckBox checkbox = (CheckBox)listView.getChildAt(j).findViewById(R.id.ColChk); 
       //your code 
     } 
    }, 500); 

2.Maybe您滾動列表視圖所以索引是錯誤的,請參考 Android ListView y position

+0

例如,如果我從Button控件的OnClickListener()中檢查函數是否正確檢查了框,但是如果我從getView()調用它,那麼總是選中其中一個複選框沒有被檢查。然後我把計數器放在getView裏面來計算有多少物品在那裏。如果我加載3個項目getView計數2但列表視圖正確顯示三個項目。 – Josef

+0

http://stackoverflow.com/questions/5687077/android-unable-to-check-all-the-checkboxes-in-a-custom-listview-because-of-rec – Gina

相關問題