4

I have an ExpandableListView with a checkbox next to group name and when expanded, child items with checkbox as well.

Let's suppose I have 4 groups with 50 childs. When a group is expanded, and I click on select all checkbox, everything works fine, all checkbox are selected and keep their state if I scroll.

But if I scroll untill the last child of the list, scrolling gets buggy after that ( scrolling to the top and then touching the screen to stop the scrolling doesn't work anymore), clicking on group doesn't work anymore too, untill I click on selectall checkbox again. Clicking on a child checkbox does nothing, I must click on selectall checkbox for everything to work.

I changed Focusable state of the child checkbox, group checkbox and tried many ways, but I can not find a solution to this. Do you have an idea of where it is coming from?

public class ExpandableListAdapter extends BaseExpandableListAdapter {


    LayoutInflater cinflater;
    LayoutInflater ginflater;
    ArrayList<Group> array;
    Context context;


    public ExpandableListAdapter(Context context, ArrayList<Group> array) {
        super();

        cinflater = LayoutInflater.from(context);
        ginflater = LayoutInflater.from(context);

        this.array = array;
        this.context = context;

    }


    public class ChildViewHolder {

        CheckBox contactcheckbox;
        TextView name;

    }


    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return array.get(groupPosition).getContacts().size();
    }


    private OnCheckedChangeListener contactchecklistener = new OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(CompoundButton checkboxView, boolean isChecked) {

                   Contact contact = (Contact) checkboxView.getTag();
                   contact.setCheck(isChecked);

               }

            };

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        ChildViewHolder cholder;

        if(convertView == null){

            cholder = new ChildViewHolder();

            convertView = cinflater.inflate(R.layout.childsitems, null);

            cholder.name = (TextView) convertView.findViewById(R.id.childnametextview);

            cholder.contactcheckbox = (CheckBox) convertView.findViewById(R.id.childcheckbox);
            cholder.contactcheckbox.setOnCheckedChangeListener(contactchecklistener);


            convertView.setTag(cholder);

        }else{

            cholder = (ChildViewHolder) convertView.getTag();

        }

        cholder.contactcheckbox.setTag(array.get(groupPosition).getContacts().get(childPosition));
        cholder.contactcheckbox.setChecked(array.get(groupPosition).getContacts().get(childPosition).isCheck());

        cholder.name.setText(array.get(groupPosition).getContacts().get(childPosition).getName());

        return convertView;
    }



    public class GroupViewHolder {
        TextView groupname;
        CheckBox groupcheck;

    }


    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return array.size();
    }

    private OnCheckedChangeListener groupchecklistener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            int index = (Integer) buttonView.getTag();

            for(int i = 0;i<array.get(index).getContacts().size();i++){
                array.get(index).getContacts().get(i).setCheck(isChecked);
            }

            array.get(index).setCheck(isChecked);

            notifyDataSetChanged();

        }
    };


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {


        GroupViewHolder gholder;

        if(convertView==null){
            gholder =  new GroupViewHolder();
            convertView = ginflater.inflate(R.layout.groupsitems,null);

            gholder.groupname = (TextView)convertView.findViewById(R.id.groupnametextview);
            gholder.groupcheckbox = (CheckBox)convertView.findViewById(R.id.groupcheckbox);
            gholder.groupcheckbox.setOnCheckedChangeListener(groupchecklistener);

            convertView.setTag(gholder);

        }else{
            gholder = (GroupViewHolder)convertView.getTag();
        }

        gholder.groupname.setText(array.get(groupPosition).getGroupname());
        gholder.groupname.setTextSize(24);
        gholder.groupname.setTextColor(Color.parseColor("#FF858585"));
        gholder.groupname.setTypeface(Typeface.DEFAULT, Typeface.BOLD);

        int paddingleft = 48;  // 38 dps
        final float scale = getResources().getDisplayMetrics().density;
        int dpvalue = (int) (paddingleft * scale + 0.5f);
        gholder.groupname.setPadding(dpvalue, 16, 0, 16);


        gholder.groupcheckbox.setTag(groupPosition);
        gholder.groupcheckbox.setChecked(array.get(groupPosition).isCheck());

        if(isExpanded == true){
                  convertView.setBackgroundDrawable(getResources().getDrawable(R.drawable.expandedbackground));
        }else{
            convertView.setBackgroundColor(Color.TRANSPARENT);
        }

        return convertView;
    }





    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}

groupsitems.xml :

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <CheckBox
        android:id="@+id/groupcheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="12dp"
        android:focusable="false"
        android:gravity="right|center"
        android:paddingRight="10dp" />

<TextView
android:id="@+id/groupnametextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/groupcheckbox"
android:focusableInTouchMode="false"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF858585"
android:textStyle="bold" />

</RelativeLayout>

childitems.xml :

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:paddingLeft="8dp" >


<TextView
        android:id="@+id/childnametextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/childcheckbox"
        android:gravity="right|center"
        android:paddingLeft="16dp"
        android:textColor="#FFAFAFAF"
        android:textSize="16sp"
        android:textStyle="bold" />

<CheckBox
        android:id="@+id/childcheckbox"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

Samet
  • 917
  • 12
  • 26
  • @ADR Where did you read that "Clicking on the last item throws Error"? I see nothing about exceptions being thrown... This edit is incorrect. – Sam Nov 17 '12 at 17:37
  • Post your relevant code, otherwise we probably won't be able to help you. – Sam Nov 17 '12 at 17:40
  • Yes there is no errors or something and I never talked about clicking last item... I didn't post the code to keep it simple but there it is. – Samet Nov 17 '12 at 19:42
  • Can you please post the whole source code. I have to implement something similar, but I am stuck! – kittu88 Apr 08 '14 at 11:30

1 Answers1

4

I've found the solution. Instead of using OnCheckedChangeListener on the groupcheckbox, I used OnClickListener and it solved everything like this :

gholder.groupcheckbox.setChecked(array.get(groupPosition).isCheck());
gholder.groupcheckbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                for(int i = 0;i<array.get(groupPosition).getContacts().size();i++){
                    array.get(groupPosition).getContacts().get(i).setCheck(gholder.groupcheckbox.isChecked());
                }

                array.get(groupPosition).setCheck(gholder.groupcheck.isChecked());

                notifyDataSetChanged();
            }
        });
Samet
  • 917
  • 12
  • 26
  • 1
    lol, you just beat me to it just as i was writing an answer. it's an annoying problem that i ran into before. basically what's happening is that your listview is locking up because it is getting `notifyDataSetChanged();` called *a lot*, too much. when you scrolled and the new checkboxes are coming into view, your method is correcting the checkboxes to show if they were checked or not. Then onCheckedChanged is called. It's a necessary predicament because you're correctly using the `viewHolder` pattern and checking for null `convertview`. as you've done, i think setting it to onClick is correct. – mango Nov 18 '12 at 00:14
  • Thanks for your help anyway. So simple but yet so hard to find. – Samet Nov 18 '12 at 00:26