1

Screnshot of When I checked first three CheckBoxes from ExpandableListView and expand first or second Item from it Then second CheckBox is automatically unchecked and last CheckBox is automatically Checked

Here is the Screenshot of it.

Here is my Custom adapter code

public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter{
Context context;
ArrayList<Group_Items> group_al;

public MyBaseExpandableListAdapter(Context context,ArrayList<Group_Items> group_al) {
    this.context=context;
    this.group_al=group_al;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<Child_Items> chList = group_al.get(groupPosition).getItems();

    return chList.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {

    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                         ViewGroup parent) {
    Child_Items ch = (Child_Items) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandable_child_items, null);
    }
    TextView child = (TextView) convertView.findViewById(R.id.child);
    TextView dd = (TextView) convertView.findViewById(R.id.dd);
    TextView date= (TextView) convertView.findViewById(R.id.date);

    child.setText(ch.getChild_title().toString());
    dd.setText(ch.getDd().toString());
    date.setText(ch.getDate().toString());

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<Child_Items> chList = group_al.get(groupPosition).getItems();

    return chList.size();
}

@Override
public Object getGroup(int groupPosition) {

    return group_al.get(groupPosition);
}

@Override
public int getGroupCount() {

    return group_al.size();
}

@Override
public long getGroupId(int groupPosition) {

    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    Group_Items gr = (Group_Items) getGroup(groupPosition);
    long group_id = getGroupId(groupPosition);

    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.expandable_group_items, null);
    }

    TextView title = (TextView) convertView.findViewById(R.id.title);
    title.setText(gr.getName());
    CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);

    return convertView;
}

@Override
public boolean hasStableIds() {

    return true;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {

    return false;
}

}

1 Answers1

0

You can use SparseBooleanArray for this

in your adapter, initialize and keep a sparse boolean array

SparseBooleanArray mSelections = new SparseBooleanArray();

in your getGroupView, do the following

@Override
public View getGroupView(int groupPosition, boolean isExpanded, 
                                   View convertView, ViewGroup parent) {
    Group_Items gr = (Group_Items) getGroup(groupPosition);
    long group_id = getGroupId(groupPosition);

    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.expandable_group_items, null);
       // adding onClick listener on CheckBox for the very first time
       CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
       chk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                mSelections.put(position, !mSelections.get(position, false));
                ((CheckBox)v).setChecked(mSelections.get(position, false));
            }

       });
    }

    TextView title = (TextView) convertView.findViewById(R.id.title);
    title.setText(gr.getName());
    CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
    chk.setTag(groupPosition);
    // reassigning checkbox to its ticked state
    chk.setChecked(mSelections.get(groupPosition, false));

    return convertView;
}
Much Overflow
  • 3,142
  • 1
  • 23
  • 40
  • I will check your answer – kalmeshwar gurav Feb 09 '16 at 08:12
  • I implement your code The problem is that when i click on Expandable list then it says Your application is _Unfortunately Stopped_ insted of expanding – kalmeshwar gurav Feb 09 '16 at 08:55
  • could you post your stack trace? – Much Overflow Feb 09 '16 at 08:59
  • I posted my stack trace you can check it – kalmeshwar gurav Feb 09 '16 at 09:22
  • ok ok I got my mistake but their is another problem when I check My first CheckBox then Automatically last checkBox is checked – kalmeshwar gurav Feb 09 '16 at 09:28
  • I have made an edit to my answer. Please check if it works for you. Make sure that the groupPosition variable passed in to getGroupView is not final – Much Overflow Feb 09 '16 at 09:47
  • If I remove _final_ from `groupPosition` Then it gives error – kalmeshwar gurav Feb 09 '16 at 10:03
  • Copy paste the entire method and it should work fine. I have removed usage of groupPosition inside anonymous class so you should not get that error! – Much Overflow Feb 09 '16 at 10:04
  • You can get my [sample app from mydrive](https://drive.google.com/file/d/0By4AGgMQHRz0ZWV4VlJ1UUxGNWM/view?usp=sharing) and check its working _Now also I facing same problem_ – kalmeshwar gurav Feb 09 '16 at 10:15
  • I do not have permission to access that link – Much Overflow Feb 09 '16 at 10:19
  • ohh sorry Please check [this link](https://drive.google.com/file/d/0By4AGgMQHRz0ZWV4VlJ1UUxGNWM/view?usp=docslist_api) – kalmeshwar gurav Feb 09 '16 at 10:21
  • Not sure why this is happening. I suspect it has something to do with the onClickListener. I have made a small update. Could you try that now? – Much Overflow Feb 09 '16 at 10:32
  • I have edited the onClickListener of the checkbox. Can you try that please? – Much Overflow Feb 09 '16 at 11:03
  • Thank you very much You saved me bro **Its Work Fine** but I have another query .. How to store checked `CheckBox` value in `database`.. I want to store only value of _title_ and _date_ textview of checked `CheckBox` – kalmeshwar gurav Feb 09 '16 at 11:08
  • That's great! Please accept this as answer if it helped you and for your other problem, please create a new question with detailed description so that others also could answer. You may have to learn to use SQLite for your next problem :) – Much Overflow Feb 09 '16 at 11:14
  • yes I will post new question but I want to store value in server side database not in SQLite, I want only hint where I should write code for store that values – kalmeshwar gurav Feb 09 '16 at 11:16
  • For that you need to publish a REST service on a server and access it from your app to store data. You can use Retrofit library for this purpose. It is fairly easy to do it if you have prior experience in developing web based data systems – Much Overflow Feb 09 '16 at 11:19
  • no no I just have only query about from where I can get checked values and send it to db – kalmeshwar gurav Feb 09 '16 at 11:27
  • You can loop through the SparseBooleanArray and fin out which positions have been marked true. Then use that position to access your grooup_item object in the main ArrayList. Refer this for an example: http://stackoverflow.com/a/8006994/3533289 – Much Overflow Feb 09 '16 at 11:33
  • sir Can I get Checked `checkbox` values from using `if (((CheckBox) v).isChecked()){ }` code in `getGroupView`?? inside the `onClickListener` of `checkBox` – kalmeshwar gurav Feb 14 '16 at 08:09
  • That would add additional complexity to your code. Since you are mapping checked items in the SparseBooleanArray, you can use that to loop through and identify checked items. Do check the link I posted above on how to do it – Much Overflow Feb 14 '16 at 09:15