2011-03-15 102 views
6

我試圖在ListActivity中執行「取消選擇所有」按鈕以取消選中由自定義SimpleCursorAdapter管理的ListView中的所有複選框。取消選中自定義ListView中的所有複選框

至於建議here,我試圖

在我ListActivity我:

Button bt_f_unsel = (Button) findViewById(R.id.btn_f_unsel); 
bt_f_unsel.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) {   
     for (int i=0; i< getListAdapter().getCount(); i++) { 
      mListView.setItemChecked(i, false); 
     } 
    }   
});   

但沒有任何反應。

我不知道這是否是因爲我的自定義行:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/contact_pic" 
     android:layout_width="50dp" 
     android:layout_height="50dp" /> 

    <TextView 
     android:id="@+id/contact_name"   
     android:textSize="10sp" 
     android:singleLine="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <CheckBox 
     android:id="@+id/checkbox" 
     android:button="@drawable/whipem_cb" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

這使得mListView.setItemChecked()找不到複選框。

如何取消選中所有cb並刷新ListActivity中按鈕的所有行?

謝謝

回答

4

老實說,我不認爲setChecked方法將與自定義佈局一起工作。它期望該視圖是一個ID爲text1的CheckedTextView。

由於視圖被回收,我認爲解決方案是更新列表中對象中的任何布爾值,以確定複選框是否被選中,然後調用adapter.notifyDataSetChanged()。您正在更改數據的布爾狀態(這是真正重要的)並告訴適配器更新ListView。因此,下一次繪製視圖時,複選框將被正確檢查。並且顯示的當前視圖將被重繪。

3

我使用的是骯髒的,但容易招:

//recursive blind checks removal for everything inside a View 
private void removeAllChecks(ViewGroup vg) { 
    View v = null; 
    for(int i = 0; i < vg.getChildCount(); i++){ 
     try { 
      v = vg.getChildAt(i); 
      ((CheckBox)v).setChecked(false); 
     } 
     catch(Exception e1){ //if not checkBox, null View, etc 
      try { 
       removeAllChecks((ViewGroup)v); 
      } 
      catch(Exception e2){ //v is not a view group 
       continue; 
      } 
     } 
    } 

} 

通過你的列表對象了。只需避免真正漫長而複雜的列表。

+0

如果我們有超過100個項目,我應該怎麼辦?它只清除其他可見項目嗎? – Google 2017-03-30 12:29:10

+0

如果這真的是ListActivity它甚至可以工作5000. – halxinate 2017-04-20 18:50:05

+0

可以建議在removeallchecks中傳遞什麼?同時調用這個方法 – Panache 2018-01-26 12:28:21

-1

我用

checkbox.setChecked(false); 
checkbox.refreshDrawableState(); 
+1

嗨,歡迎來到Stack Overflow。如果您突出顯示代碼並單擊「{}」按鈕,它將自動爲您突出顯示。 – Ben 2013-08-21 21:28:08

2

這爲我工作:

MenuViewAdapter adapter = new MenuViewAdapter(this, menuViews,this); 
ListView lv = (ListView)this.findViewById(R.id.menu_list); 


CheckBox cb; 

for(int i=0; i<lv.getChildCount();i++) 
{ 
    cb = (CheckBox)lv.getChildAt(i).findViewById(R.id.checkBox); 
    cb.setChecked(false); 
} 
adapter.notifyDataSetChanged(); 
+0

使用此代碼,我可以取消選中只有可見覆選框,如果我有超過100個項目。您是否嘗試過取消選中列表中的第一項和最後一項? – Google 2017-03-08 11:06:17

+0

這不取消選中列表中的所有複選框 – hrushi 2017-03-28 22:55:47

+0

我做了相同的技巧,但無法取消選中所有複選框。我沒有檢查只有可見的項目。但是當我向下滾動列表時,某些複選框不會取消選中 – Google 2017-03-30 12:27:47

相關問題