2011-08-27 129 views
1

我使用ExpandableListView。在小组和小孩中,都有复选框。如何做到以下要求?检查ExpandableListView(安卓)所有复选框

当用户检查组的复选框,检查其孩子的所有框。

我想我需要覆盖BaseExpandableListAdapter, 但我不知道应该填写什么。谢谢。

public View getGroupView(int groupPosition, boolean isExpanded, 
    View convertView, ViewGroup parent) 
{ 
    CheckBox cb = (CheckBox)v.findViewById(R.id.checkBox1); 
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
      boolean isChecked) 
     { 
      //Fill in what? 
     } 

    }); 

} 

也有同样的问题,但我不明白它的解决方案: How to get all childs view in group with ExpandableListView

回答

2

链接的答案,我认为是说你应该保持跟踪你的检查组的状态的一个数组列表。然后,在你的BaseExpandableListAdapter您getChildView方法,你会做检查,以查看是否为子组存在..

这里是我的实现,我使用一个HashMap,而不是一个ArrayList:

public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    CheckBox check__bookmark = (CheckBox)child_row.findViewById(R.id._checkBox); 

    if (mapp.group_bookmark_s_checked.containsKey((groupPosition))==true)    
     check__bookmark.setChecked(true); 
    else 
     check__bookmark.setChecked(false); 

}

public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 

    CheckBox PlayGroupSelected = (CheckBox)group_row.findViewById(R.id.bookmark_group_checkBox);   

    if (mapp.group_bookmark_s_checked.containsKey((groupPosition))==true)    
     PlayGroupSelected.setChecked(true); 
    else 
     PlayGroupSelected.setChecked(false); 
    PlayGroupSelected.setOnCheckedChangeListener(new BookmarkGroupCheckedChangeListener(groupPosition, mapp, group_row)); 

现在您的听众通过删除和添加到您的状态对象管理的状态。我用notifyDataSetChanged(),因为孩子们不刷新一次的检查状态变化绘制。不知道如果这是解决问题的正确方法,但它的伎俩。

class BookmarkGroupCheckedChangeListener implements OnCheckedChangeListener { 

    private int mPosition; 
    MyApplication mapp; 
    RelativeLayout mgroup_row; 


    BookmarkGroupCheckedChangeListener(int position, MyApplication app, RelativeLayout group_row) { 
     mapp = app; 
     mPosition = position; 
     mgroup_row = group_row; 
    } 

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

    if (isChecked == true) { 
     mapp.group_bookmark_s_checked.put((mPosition), new Boolean(true)); 
     Log.v("DEBUG:", "Group Bookmark Checked On at " + mPosition); 
     notifyDataSetChanged(); 
    } 
    else if (mapp.group_bookmark_s_checked.containsKey(mPosition)){ 
     mapp.group_bookmark_s_checked.remove((mPosition)); 
     Log.v("DEBUG:", "Checked Off at " + mPosition); 
     notifyDataSetChanged(); 
    } 


    } 

} 
+0

是。 notifyDataSetChanged();很重要。坦克 – IndieBoy