如何从ListView android中获取复选框?

问题描述:

荫使用下面让复选框从OnItemClickListener代码片段如何从ListView android中获取复选框?

onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     Log.w("TAG","onItemClick clicked position :"+position); 

     CheckBox cbx = (CheckBox)view.findViewById(R.id.c_checkbox); 
     if(cbx.isChecked()){ 
      Toast.makeText(getApplicationContext(), 
        "Checked position " + shoppingList.get(position).getItem(), 
        Toast.LENGTH_SHORT).show(); 
     } 

我需要让所有的位置,以检查其列表项是checked.For是 我下面用下面的代码片断

int firstPosition = list.getFirstVisiblePosition(); 
      for(int i=firstPosition;i<=list.getCount();i++){ 
      View v=list.getChildAt(i); 
      cbx = (CheckBox)v.findViewById(R.id.c_checkbox); 
      if(cbx.isChecked()){ 
      } 
     } 

cbx给我空指针exception.i使用ViewHolder为自定义adapter.Please给我的解决方案。为什么同样的事情在OnItemClickListener工作?

问候, Rajendar

它认为它可能是

for(int i=firstPosition;i< **=** list.getCount();i++) 

但也只是胡乱猜测。

为什么不使用可检查的listview然后使用this

+0

我想在左边的复选框 – ADIT 2010-12-14 10:45:34

+0

我在ViewHolder中犯了一个错误,并且在错误的情况下给出了“i ADIT 2010-12-14 10:47:07

+0

现在很高兴:) – mad 2010-12-14 10:51:04