Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7557751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:12:51+00:00 2026-05-30T12:12:51+00:00

I have an ExpandableListView backed by a BaseExpandableListAdapter. The child list items are all

  • 0

I have an ExpandableListView backed by a BaseExpandableListAdapter. The child list items are all CheckBoxes.

I expand a group, check a checkbox, and then collapse the group. The checkbox is then unchecked.

My log output is:

-- I check the checkbox:
02-27 13:51:29.674: D/ExpandableListAdapter(3583): Option '2' CHECKED
--- I collapse the group, which unchecks the checkbox:
02-27 13:51:30.850: D/ExpandableListAdapter(3583): Option '2' UNCHECKED

As you can see, collapsing a group is calling (possibly indirectly) my OnCheckedChangedListener on all of the checkboxes in that group.

My code is:

public class TestExpandableListActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExpandableListView elv = 
            (ExpandableListView) findViewById(R.id.optionsELV);
        ExpandableListAdapter ela = new ExpandableListAdapter(this);
        elv.setAdapter(ela);
    }
    public class ExpandableListAdapter extends BaseExpandableListAdapter {
        private static final String TAG = "ExpandableListAdapter";
        private final String[] groups;
        private final String[][] children;
        private final boolean[][] childrenChecked;
        private final Context context;
        public ExpandableListAdapter(Context c) {
            this.context = c;
            groups = new String[] { "Options A", "Options B" };
            children = 
                new String[][] {{"A", "B", "C"},{"1", "2", "3"}};
            childrenChecked = 
                new boolean[][] {{false, false, false},{false, false, false}};
        }
        public View getChildView(
                final int groupPos, 
                final int childPos,
                boolean isLastChild, 
                View convertView, 
                ViewGroup parent) {
            CheckBox cb = new CheckBox(context);
            cb.setText(children[groupPos][childPos]);
            cb.setChecked(childrenChecked[groupPos][childPos]);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(
                        CompoundButton buttonView, 
                        boolean isChecked) {
                    Log.d(TAG, 
                            String.format("Option '%s' %s", 
                                    children[groupPos][childPos], 
                                    isChecked ? "CHECKED" : "UNCHECKED"));
                    childrenChecked[groupPos][childPos] = isChecked;
                }
            });
            return cb;
        }
        public int getGroupCount() {
            return groups.length;
        }
        public int getChildrenCount(int groupPos) {
            return children[groupPos].length;
        }
        public String getGroup(int groupPos) {
            return groups[groupPos];
        }
        public String getChild(int groupPos, int childPos) {
            return children[groupPos][childPos];
        }
        public long getGroupId(int groupPos) {
            return (1024 * (groupPos + 1));
        }
        public long getChildId(int groupPos, int childPos) {
            return (1024 * (groupPos + 1)) + (childPos + 1);
        }
        public View getGroupView(
                int groupPos, 
                boolean isExpanded, 
                View convertView, 
                ViewGroup parent) {
            TextView tv = new TextView(context);
            tv.setText(groups[groupPos]);
            return tv;
        }
        public boolean hasStableIds() {
            return true;
        }
        public boolean isChildSelectable(int groupPos, int childPos) {
            return true;
        }
    }
}

My layout (note that I’ve properly set choiceMode to multipleChoice) is:

<?xml version="1.0" encoding="utf-8"?>
<ExpandableListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/optionsELV"
    android:choiceMode="multipleChoice"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Why is collapsing a group unchecking all of my checkboxes? How can I prevent this from happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:12:52+00:00Added an answer on May 30, 2026 at 12:12 pm

    I think I’ve found the solution — use a CheckedTextView instead of a CheckBox.

    public View getChildView(
            final int groupPos, 
            final int childPos,
            boolean isLastChild, 
            View convertView, 
            ViewGroup parent) {
        final CheckedTextView ctv = new CheckedTextView(context);
        ctv.setChecked(childrenChecked[groupPos][childPos]);
        ctv.setCheckMarkDrawable(
                ctv.isChecked()
                ? android.R.drawable.checkbox_on_background 
                : android.R.drawable.checkbox_off_background);
        ctv.setText(children[groupPos][childPos]);
        ctv.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                ctv.toggle();
                Log.d(TAG, 
                    String.format("Option '%s' %s", 
                        children[groupPos][childPos], 
                        ctv.isChecked() ? "CHECKED" : "UNCHECKED"));
                childrenChecked[groupPos][childPos] = ctv.isChecked();
                ctv.setCheckMarkDrawable(
                        ctv.isChecked() 
                        ? android.R.drawable.checkbox_on_background 
                        : android.R.drawable.checkbox_off_background);
            }
        });
        return ctv;
    }
    

    The CheckedTextView doco does say that CheckedTextView is useful in a ListView — “this is useful when used in a ListView where the it’s setChoiceMode has been set to something other than CHOICE_MODE_NONE” — but I’m pretty unclear about what the difference is between CheckBox and CheckedTextView, and on why CheckBoxes just seem to completely not work.

    Can anyone tell me what the difference is between CheckBox and CheckedTextView, and why CheckBoxes function so oddly inside an ExpandableListView?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an expandablelistview with a checkbox by group and a checkbox by child.
If i have a expandablelistview with custom layout (group.xml and child.xml), the are any
I have created an ExpandableListView with the following code: <ExpandableListView android:id=@+id/android:list android:layout_width=wrap_content android:layout_height=0dp android:layout_marginTop=5dp
can we have ExpandableListView inside ListView so that I can have group level first
I have a ExpandableListView in which i am loading custom layouts in both group
I have ExpandableListView and I extended BaseExpandableListAdapter for my own adapter. I am having
ok so I have an ExpandableListView, I have defined all my views and everything
I have an AlertDialog populated by an ExpandableListView. The list itself works perfectly, but
I have a Dynamic ExpandableListView. That is to say child are only load when
I have to delete child item in my expandablelistview. My attempt was: 1. Data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.