Uncheck all checkboxes in a custom ListView

前端 未结 4 689
无人及你
无人及你 2021-01-02 21:36

I\'m trying to do an \"Unselect all\" button in a ListActivity to unchecked all checkboxes in a ListView managed by a custom SimpleCursorAdapter.

As suggested here,

相关标签:
4条回答
  • 2021-01-02 21:55

    I use

    checkbox.setChecked(false);
    checkbox.refreshDrawableState();
    
    0 讨论(0)
  • This worked for me:

        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 讨论(0)
  • 2021-01-02 22:04

    I'm using a dirty but easy trick:

    //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;
                }
            }
        }
    
    }
    

    Pass your list object to it. Just avoid really long and complicated lists.

    0 讨论(0)
  • 2021-01-02 22:17

    Honestly I don't think the setChecked Methods will work with a custom layout. It expects the view to be a CheckedTextView with an id of text1.

    And since the views are recycled I think the solution is to update whatever boolean in your objects in the list that determines if the checkbox is checked and then call adapter.notifyDataSetChanged(). You are changing the boolean state of the data (which is what really matters) and telling the Adapter to update the ListView. So the next time the views are drawn the checkbox will be checked correctly. And the current views that are shown will be redrawn.

    0 讨论(0)
提交回复
热议问题