Search code examples
androidcheckboxexpandablelistview

CheckBox in ChildView of ExpandableListView


In my Android application, I have an ExpandableListView with each group having only one child, which contains a checkbox. I'm having trouble in setting the value of the checkboxes.

I'm retrieving the values of checkbox from database and storing it in an integer ArrayList with values 0 & 1 for checked and unchecked respectively

ArrayList<Integer> trustedList = new ArrayList<Integer>();

Initially, I've put all the entries in database as 1.

The Adapter class for ExpandableListView is as follows :

class DataAdapter extends BaseExpandableListAdapter {

    private Context context;
    LayoutInflater lat = getLayoutInflater();

    CheckBox cb;

    public DataAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

@Override
    public View getChildView(int group, int child, boolean arg2, View v,
            ViewGroup arg4) {
        // TODO Auto-generated method stub

        v = lat.inflate(R.layout.inflate_list, null);
        cb = (CheckBox) v.findViewById(R.id.checkBox1);

        if (trustedList.get(group) == 1)
            cb.setChecked(true);
        else
            cb.setChecked(false);

    return v;
 }
}

But I'm getting unchecked values for the checkboxes. Also, on checking the checkbox and collapsing and expanding the group, I'm getting unchecked checkbox again.


Solution

  • class DataAdapter extends BaseExpandableListAdapter {
    
        private Context context;
        LayoutInflater lat = getLayoutInflater();
    
        CheckBox cb;
    
        public DataAdapter(Context context) {
            // TODO Auto-generated constructor stub
            this.context = context;
        }
    
    @Override
        public View getChildView(int group, int child, boolean arg2, View v,
                ViewGroup arg4) {
            // TODO Auto-generated method stub
    
            v = lat.inflate(R.layout.inflate_list, null);
            cb = (CheckBox) v.findViewById(R.id.checkBox1);
    
            if (trustedList.get(group) == 1)
                cb.setChecked(true);
            else
                cb.setChecked(false);
    cb.setOnCheckChangeListener(new CheckchangeListener(group) );
    
        return v;
     }
    class CheckchangeListener implements OnCheckedChangeListener {
            private int position;
    
            public CheckchangeListener(int position) {
                // TODO Auto-generated constructor stub
    
                this.position= position;
    
            }
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    //updateyour list and database here
    
                } else {
                    //updateyour list and database here
    
                }
    
            }
        }
    
    
    }