简体   繁体   中英

Issues in Expandable listview with checkbox

My Requirement is when I select parent CheckBox selected child CheckBoxes simultaneously.But I'm clicking on parent checkbox it not selected child checkbox.how to do this ? can any on give suggestions.

Here is my code

   public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
    final String headerTitle = (String)getGroup(groupPosition);
    ParentViewHolder cv = null;
    if (convertView == null)
    {
        LayoutInflater li = (LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.group_list,null);
      cv  = new ParentViewHolder();
       cv.text = (TextView)convertView.findViewById(R.id.groupTitle);
        cv.text.setTypeface(null, Typeface.BOLD);
        cv.text.setText(headerTitle);
        cv.parentCheckbox =       (CheckBox)convertView.findViewById(R.id.groupCheckbox);

        convertView.setTag(cv);
        cv.parentCheckbox.setTag(groupPosition);
        cv.parentCheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((CheckBox)v).isChecked())
                {
                    Log.e("TAG","clicked");
                        groupitem.itemClick(groupPosition, true);
                    Toast.makeText(Context.getApplicationContext(),   headerTitle+"success", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    else
    {

    }

        return convertView;
} 

I have make some logic same as your requirement. you can make this like below:

 /*******************
 * Checkbox Checked Change Listener
 ********************/

private final class CheckUpdateListener implements CompoundButton.OnCheckedChangeListener {
    private final ParentModel parent;
    private  final ArrayList<ChildModel> childs;

    private CheckUpdateListener(ParentModel parent,ArrayList<ChildModel> childs) {
        this.parent = parent;
        this.childs=childs;


    }


    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.i("onCheckedChanged", "isChecked: " + isChecked);


        parent.setChecked(isChecked);
        for(int i=0;i<childs.size();i++)
        {
            childs.get(i).setChildChecked(isChecked);
        }
        Log.i("TAG", "CHILD SIZE=>" + parent.getChildList().size());


        notifyDataSetChanged();


    }
}


private class ChildCheckUpdateListener implements CompoundButton.OnCheckedChangeListener {
    private final ParentModel parentModel;
    private final ChildModel childModel;

    public ChildCheckUpdateListener(ChildModel child, ParentModel model) {
        this.parentModel = model;
        this.childModel = child;
    }

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

        if (!parentModel.isChecked()) {
            childModel.setChildChecked(isChecked);
        }
        notifyDataSetChanged();
    }
}

you just need to call it from getGroupView and getChildview method like below:

from Group View

  groupViewHolder.chkbox.setOnCheckedChangeListener(new CheckUpdateListener(parent,childs));

from ChildView

childViewHolder.childChk.setOnCheckedChangeListener(new ChildCheckUpdateListener(child, model));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM