2014-04-19 75 views
0

我有方法来选择listView中的所有复选框,有时复选框没有工作。当我滚动到最后一个项目并返回时,会发生这种情况。ListView selecioint所有复选框

这梅托德呼叫活动:

public void setAllBoxesChecked(boolean value) 
    { 
     for(int i=0; i < lvSklepy.getCount(); i++) 
     { 
      View view = lvSklepy.getAdapter().getView(i, null, lvSklepy); 

      CheckBox cb = (CheckBox) view.findViewById(R.id.cb_sklep); 
      cb.setChecked(value); 
     } 
    } 

适配器: 第一项是选择所有复选框复选框。

public View getView(int position,View convertView, final ViewGroup parent) 
    { 
.... 
final CheckBox chboxShop = (CheckBox) convertView.findViewById(R.id.cb_sklep); 

     if(position == 0) 
     { 

      chboxShop.setOnCheckedChangeListener(new OnCheckedChangeListener() 
      { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
       { 
        Boolean blnObj = new Boolean(isChecked); 

        callOnClickListener(RES_CHECKBOX, blnObj, chboxShop); 
       } 
      }); 
      rLayout = R.layout.ad_preferowane_sklepy; 
     } 
     else 
     { 
      final ImageLayout llImage = (ImageLayout) convertView.findViewById(R.id.il_image); 

      chboxShop.setText(item.nazwaPelna); 
      llImage.imageFromWeb(item.logoUrl); 


     } 
... 
} 
+0

一个列表,并从getView方法读取。 –

+0

我使用“技巧”添加私人布尔状态到适配器。在活动中添加新项目并从阵列列表中删除最后一项(适配器),然后调用notifyDataSetChanged。适配器刷新并且checbox从状态设置值 – michael

回答

0

在有你的列表视图布局添加以下代码

android:descendantFocusability="blocksDescendants" 

的XML文件,比方说你有一个文本视图相对布局 - 你需要保存在经过价值

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:descendantFocusability="blocksDescendants"> 



<TextView 
    android:id="@+id/look_separator" 
    style="?android:attr/listSeparatorTextViewStyle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@android:color/white" 

    /> 

<CheckBox 
    android:id="@+id/look_checkbox" 
    android:layout_width="wrap_content" 
    android:layout_alignParentEnd="true" 

    />