0

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;
} 
PriyankaChauhan
  • 953
  • 11
  • 26
A.asha
  • 45
  • 6

1 Answers1

0

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));
Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
  • when i click on first parent item it shows correct list.when i click on second parent item it shows second parent item data in first and second parent how to resolve – A.asha Jul 26 '16 at 07:50
  • @A.asha sorry.I didn't get you what exactly problem you facing – Vishal Thakkar Jul 26 '16 at 07:59
  • suppose Foodmenu is parent item in that burger,piza are list submenu and one more parent item Shop menu in that veg shop,non veg shop are list submneu.Now when i click on Foodmenu it shows correct data .next i click on Shopmenu it shows shop menu data but in Foodmenu daata is changed it shows shop menu data – A.asha Jul 26 '16 at 08:15
  • then you make mistake in getchildview method – Vishal Thakkar Jul 26 '16 at 08:54