lundi 29 août 2016

Select all issue in Parent and Child Checkbox

Well My task is to perform parent and child Checkbox in recyclerview like there are 4 states and all states have there few cities.Now problem is When I checked in parent(State) but properly cities are not get selected.lets say Gujarat State have surat,ahmedabad cities but when we select Gujarat checkbox it got select all in other state child maharastra cities.

Here is a code(this code is in master Custom adapter):

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_row, parent, false); MyViewHolder myViewHolder = new MyViewHolder(itemview);

    return myViewHolder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    dynamiclist1();
    final Employee emp = list.get(position);

    holder.setIsRecyclable(false);
    holder.tv.setText(emp.getName());
    if (emp.getName() == "Gujarat") {
        holder.chkbox_parent.setTag("Gujarat");

    }
    holder.rv.setHasFixedSize(true);

    LinearLayoutManager llm = new LinearLayoutManager(context);
    holder.rv.setLayoutManager(llm);
    holder.rv.setHasFixedSize(true);
    holder.chkbox_parent.setTag(emp.getName());
    if (emp.getName() == "Gujarat") {
        cuChild = new CustomAdapter_child(context, child1, "Gujarat");
    } else {
        cuChild = new CustomAdapter_child(context, child1, "");

    }
    holder.rv.setAdapter(cuChild);
    holder.chkbox_parent.setTag(emp.getName());
    holder.chkbox_parent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            Toast.makeText(context, "" + holder.chkbox_parent.getTag(), Toast.LENGTH_SHORT).show();
            try {
                RecyclerView rv1 = (RecyclerView) itemview.findViewById(R.id.rv2);
                Toast.makeText(context, "" + rv1.getChildCount(), Toast.LENGTH_SHORT).show();
                CheckBox cb;
                TextView txt;
                for (int i = 0; i < rv1.getChildCount(); i++) {



                    cb = (CheckBox) rv1.getChildAt(i).findViewById(R.id.chkbox_child);
                    //txt = (TextView) rv1.getChildAt(i).findViewById(R.id.tv2);
                    Toast.makeText(context, "" + cb.getText(), Toast.LENGTH_SHORT).show();

                        cb.setChecked(holder.chkbox_parent.isChecked());

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}

(this is child Custom adapter):

public void onBindViewHolder(MyViewHolder holder, int position) {

    Employee_Child emp = list.get(position);
    holder.tv.setText(emp.getName1().concat("-").concat(this.TAG ).concat(""+position));

    if (this.TAG == "Gujarat") {
        holder.chkchild.setText("Gujarat");
    }

}




Aucun commentaire:

Enregistrer un commentaire