开发者

Check all checkbox in ExpandableListView (Android)

开发者 https://www.devze.com 2023-03-31 09:02 出处:网络
I am using ExpandableListView. In both group and child, there are checkboxes. How to do the following requirement?

I am using ExpandableListView. In both group and child, there are checkboxes. How to do the following requirement?

When user che开发者_开发问答ck the checkbox of group, check all the box of its child.

I think I need to overwrite the BaseExpandableListAdapter, but I don't know what should be filled in. Thank You.

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?
        }

    });

}

There is a similar question, but I don't understand its solution: How to get all childs view in group with ExpandableListView


The linked answer I believe is saying you should keep track of the state of your checked groups in an array list. Then, in your getChildView method for your BaseExpandableListAdapter you would do a check to see if the group for the child is present.

Here is my implementation, I'm using a hashmap instead of an 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));
}

Now in your listener you manage the state by removing and adding to your state object. I used notifyDataSetChanged() because the children weren't refreshing once drawn on the check state change. Not sure if that was the right way to solve that problem but it did the trick.

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();
        }

    }

}


Here's a link:http://prativas.wordpress.com/category/android/expandable-list-view-in-android/ that contains everything that you need. Just go through the code and try it.

0

精彩评论

暂无评论...
验证码 换一张
取 消