2013-07-29 43 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

相关问题