0

Screenshot of checked 3 checkboxes

Screenshot of automatically checked last Checkbox

I checked the first three checkbox on expandableListView. Why when I expand listview is the last CheckBox checked?

private ArrayList<Group_Items> setAllData() {

    String group_titles[] = {
            "Payment of Employer and employees contribution",
            "Monthly Return Filing in Form 5 and Declaration in form 2",
            "Monthly Return Filing in Form 10",
            "Monthly Return Filing in Form 12",
            "Statement of Recovery from contractor",
            "Annual Return in Form 6A",
            "Renewal of contribution card of an employee by filing Form 3 & 3A",
            "Submission of contribution card of employees leaving services" };

    String child_info[] = {
            "We have deployed secured web services framework in accordance with OASIS standards. Please refer the user manual (Secured Web service user manual) under the help section and do the necessary changes at your end to start consuming the same. Existing web services shall be discontinued w.e.f 1-Dec-2015.",
            "Return of employees qualifying for membership",
            "Return of members leaving the services during the month",
            "Statement of contribution",
            "Self Explnatory",
            "Consolidated annual contribution statement",
            "Self Explnatory",
            "Self Explnatory" };

    String dates[] = {
            "15th of Next Month + 5 Days Grace period",
            "15th of Next Month",
            "15th of Next Month",
            "25th of Next Month",
            "7th of Next Month",
            "30th April",
            "Within one month on the expiry of contribution card currancy to the commissioner",
            "20th of Next Month" };

    ArrayList<Group_Items> list = new ArrayList<Group_Items>();
    ArrayList<Child_Items> ch_list= new ArrayList<Child_Items>();
    ch = new Child_Items();
    int i=0;
    int size=1;

    for (String group_title : group_titles) {
        Group_Items gru = new Group_Items();

        gru.setName(group_title);

        ch_list = new ArrayList<Child_Items>();
        for (; i < size; i++) {
            Child_Items ch = new Child_Items();

            ch.setChild_title(child_info[i]);
            ch.setDd("Due Date:");
            ch.setDate(dates[i]);

            ch_list.add(ch);
        }

        gru.setItems(ch_list);
        list.add(gru);
        size=size+1;
    }
    return list;
}

I have created ExpandableListView with CheckBox beside each title and when I checked first three or four CheckBox from eight titles and first groupItem is expanded then automatically the last (8th) CheckBox is checked.

This 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;
}

}
Community
  • 1
  • 1
  • Show the code of you adapter. Most likely, you don't properly set the CheckBox state in you adapters getView(). The adapter recycles and reuses views, so when you scroll down, it will take one that has been used before at the top of the list and it is thus checked. – metter Feb 08 '16 at 10:14
  • @metter I given my whole custom adapter class please check it –  Feb 09 '16 at 06:26
  • As I assumed, the problem here is that when you check the first item and then scroll it out of view, Android will re-use that item and add it to the bottom of the list. In your adapter, you then change the title of that item, but you do not set the correct state of the checkbox. Your model needs to contain a field whether or not the item is checked. Then, in your getGroupView method, find the Checkbox item and set it to checked or unchecked depending on your model. Edit: The some goes the other way. When you check an item, you will need to react to that and set the model value accordingly. – metter Feb 10 '16 at 14:40
  • Have you try the solution from this link? http://stackoverflow.com/a/6172078/6318215 – Kristin.Y Jun 23 '16 at 04:43

0 Answers0