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 5956151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:12:57+00:00 2026-05-22T18:12:57+00:00

Friends, I am trying to write a application which use checkbox in ExpandableListView, I

  • 0

Friends,

I am trying to write a application which use checkbox in ExpandableListView, I got a problem here which is maintaining checkbox state of the application, I got the example from here , my problem is maintaining checked state of the checkboxes, whenever I check one of the checkboxes and Expand the List, the checkboxes do not have the checked state where they supposed to have. I have try to maintain by adding ArrayList to store the position of the store and reload the position in getChildView() but still not achieve what I want to do. here is my code

public class ElistCBox extends ExpandableListActivity {

    private static final String LOG_TAG = "ElistCBox";
    ArrayList<String > chkState = new ArrayList<String>();
    static final String colors[] = {"grey","blue","yellow","red"};
    static final String shades[][] ={ { "lightgrey","#D3D3D3","dimgray","#696969", "sgi >gray 92","#EAEAEA"},
        { "dodgerblue 2","#1C86EE","steelblue >2","#5CACEE","powderblue","#B0E0E6"},
        { "yellow 1","#FFFF00", "gold 1","#FFD700","darkgoldenrod 1"," #FFB90F" },
        {"indianred 1","#FF6A6A", "firebrick 1","#FF3030", "maroon","#800000" } };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main); 
        SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter( 
            this,
            createGroupList(),
            R.layout.group_row, new String[] { "colorName" },
            new int[] { R.id.childname }, createChildList(),
            R.layout.child_row,
            new String[] { "shadeName", "rgb" },
            new int[] { R.id.childname, R.id.rgb }
        ) { 
            @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
            {
                final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);

                final CheckBox chkColor = (CheckBox)v.findViewById(R.id.check1);

                if(chkState.contains(groupPosition+","+childPosition)){
                    chkColor.setChecked(true);
                }else{
                    chkColor.setChecked(false);
                }
                chkColor.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e("Checkbox Onclick Listener", Integer.toString(groupPosition) + " - " + Integer.toString(childPosition));
                    }
                });
                chkColor.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        Log.e("Checkbox check change Listener", Integer.toString(groupPosition) + " - " + Integer.toString(childPosition));
                        if(chkColor.isChecked()){
                            chkState.add(groupPosition+","+childPosition);
                        } else {
                            chkState.remove(groupPosition+","+childPosition); 
                        }
                    }
                });
                return super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
            }
        };
        setListAdapter( expListAdapter );
    }

    public void onContentChanged  () {
        super.onContentChanged();
        Log.e( LOG_TAG, "onContentChanged" );
    }

    public boolean onChildClick(
            ExpandableListView parent, 
            View v, 
            int groupPosition,
            int childPosition,
            long id) {
        Log.e( LOG_TAG, "onChildClick: "+childPosition );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        if( cb != null )
        cb.toggle();
        return false;
    }

    public void  onGroupExpand  (int groupPosition) {
        Log.e( LOG_TAG,"onGroupExpand: "+groupPosition );
    }

    private List createGroupList() {
        ArrayList result = new ArrayList();
        for( int i = 0 ; i < colors.length ; ++i ) {
            HashMap m = new HashMap();
            m.put( "colorName",colors[i] );
            result.add( m );
        }
        return (List)result; 
    }

    private List createChildList() {
        ArrayList result = new ArrayList();
        for( int i = 0 ; i < shades.length ; ++i ) {
            ArrayList secList = new ArrayList();
            for( int n = 0 ; n < shades[i].length; n += 2 ) {
                HashMap child = new HashMap();
                child.put( "shadeName", shades[i][n] );
                child.put( "rgb", shades[i][n+1] ); secList.add( child );
            }
            result.add( secList );
        } 
        return result;
    }
}
  • 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-22T18:12:58+00:00Added an answer on May 22, 2026 at 6:12 pm

    this is my answer to the this issue, u need to keep a check state for individual child and check on it when the the child is clicked. after some research, this is due to android’s view lifecycle causing the view to refresh thus not keeping the state.

    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    //Variables
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; //headers
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };
    private String[] group_vaues = {"PN", "DN", "CN", "FN"};
    private String[][] children_values = {
            { "Ar", "Ba", "Ch", "Da" },
            { "Ace", "Ban", "Cha", "Deu" },
            { "Flu", "Snu" },
            { "Gol", "Bub" }
    };
    ArrayList<ArrayList<Integer>> check_states = new ArrayList<ArrayList<Integer>>();
    
    private Context context;
    
    //Constructors
    public MyExpandableListAdapter() {
    
    }
    
    public MyExpandableListAdapter(Context c) {
        this.context = c;
    }
    
    //Set Methods
    public void setGroupsAndValues(String[] g, String[] v) {
        this.groups = g;
        this.group_vaues = v;
    }
    
    public void setChildrenAndValues(String[][] c, String[][] v) {
        this.children = c;
        this.children_values = v;
        //initialize the states to all 0;
        for(int i = 0; i < c.length; i++) {
            ArrayList<Integer> tmp = new ArrayList<Integer>();
            for(int j = 0; j < c[i].length; j++) {
                tmp.add(0);
            }
            check_states.add(tmp);
        }
    }
    
    //Get Methods
    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }
    
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }
    
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        View grid;
    
    
       LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       grid = inflater.inflate(R.layout.specialty_list_item, parent, false);
    
       final int grpPos = groupPosition;
       final int childPos = childPosition;
    
        TextView header = (TextView)grid.findViewById(R.id.title);
        header.setText(getChild(groupPosition, childPosition).toString());
        final View tick = grid.findViewById(R.id.image);
        if(check_states.get(grpPos).get(childPos) == 1)
            tick.setVisibility(View.VISIBLE);
        else
            tick.setVisibility(View.GONE);
    
        grid.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                check_states.get(grpPos).set(childPos, 1);
                tick.setVisibility(View.VISIBLE);
            }
        });
    
        return grid;
    }
    
    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }
    
    public int getGroupCount() {
        return groups.length;
    }
    
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        View grid;
    
        if(convertView==null){
           grid = new View(context);
           LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           grid = inflater.inflate(R.layout.specialty_header, parent, false);
        }else{
           grid = (View)convertView;
        }
    
        TextView header = (TextView)grid.findViewById(R.id.specialty_header);
        header.setText(getGroup(groupPosition).toString());
    
    
        return grid;
    }
    
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    
    public boolean hasStableIds() {
        return true;
    }
    

    }

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

Sidebar

Related Questions

I'm trying to write an application that fetches a list of a user's friends
I am trying to use qt to write an application that connects back to
I'm trying to write my own logging class and use it as a stream:
I am trying to write an operator which converts between the differnt types of
I'm struggling with some SQL/php syntax issues here. I'm trying to write two functions.
So I'm trying to use the new batches functionality to the graph API, which
I'm trying to write a simple application that finds out who liked your page.
I've got part of code from friend and I'm trying to understand it and
hi friends iam trying to upload a file from the android device to the
My friend is trying to write a Windows application that can query an iPhone

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.