我的任务是在recyclerview中执行父和子Checkbox,就像有4个州,所有州都有少数城市。**现在的问题是当我检查父(州)但正确的城市没有被选中时。说古吉拉特邦有surat,艾哈迈达巴德城市,但是当我们选择古吉拉特邦复选框时,它会在其他州的儿童玛哈特城市中选择所有城市**。
这是一个代码(此代码在主自定义适配器中):
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();
}
}
});
}
(这是子自定义适配器):
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");
}
}