jeudi 22 juin 2017

After programmatically checking checkboxes in recyclerview in android, other random checkboxes getting checked automatically

I have one recyclerview in my android application, listing some records with checkboxes. While selecting records by programatically checking checkboxes, I found that once checked records, other records from recyclerview automatically get selected. And sometimes the previously selected checkbox get unselected automatically

adapter_for_group.java

public class adapter_for_group extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private LayoutInflater layoutInflater;
static List<GetandSet> grpinbuiltnme;
static List<GetandSet> linknmed;
private static Context context;
public static ArrayList<String> listcheck = new ArrayList<>();

public adapter_for_group(Context context, List<GetandSet> links, List<GetandSet> nmelist) {
    this.context = context;
    layoutInflater = LayoutInflater.from(context);
    this.grpinbuiltnme = nmelist;
    this.linknmed = links;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = layoutInflater.inflate(R.layout.url_recycler, parent, false);
    adapter_for_group.Holder holder = new adapter_for_group.Holder(v);
    return holder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final adapter_for_group.Holder holder1 = (adapter_for_group.Holder) holder;
    final GetandSet getandSet = linknmed.get(position);
    holder1.name.setText(getandSet.getName());
holder1.checkedclickListener(new Group.ItemCheckedClickListenered() {
        @Override
        public String onCheckedClick(View view, int position) {
            if(listcheck.contains(linknmed.get(position).getName())/*&& dsaf.listcheck1.contains(linklst.get(position).getName())*/){
                listcheck.remove(linknmed.get(position).getName());
            }
            else {
                listcheck.add(linknmed.get(position).getName());
            }
            return null;
        }
    });
}

@Override
public int getItemCount() {
    return linknmed.size();
}

class Holder extends RecyclerView.ViewHolder {
    TextView name;
    LinearLayout ll;
    CheckBox[] ch;
    int i,kl;
    ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();
    private Group.ItemCheckedClickListenered checkedclickListenered;

    public Holder(final View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.tab_report_vehicle);
        ll = (LinearLayout) itemView.findViewById(R.id.l1);
        ll.setVisibility(View.VISIBLE);
        ch = new CheckBox[linknmed.size()];
        for (GetandSet getandSet1 : linknmed) {
            ch[i] = new CheckBox(context);
            ch[i].setId(getandSet1.get_id());
            ll.addView(ch[i]);
            mCheckBoxes.add(ch[i]);
            i++;
        }
        itemView.setTag(itemView);

        try {
            if (grpinbuiltnme != null) {
                for (GetandSet getandSet : grpinbuiltnme) {
                    for (kl = 0; kl <= mCheckBoxes.size() - 1; kl++) {
                        if (getandSet.get_id() == mCheckBoxes.get(kl).getId()) {
                            if (ch[kl].isChecked()){
                                ch[kl].setChecked(false);
                            }else {
                                ch[kl].setChecked(true);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        i = 0;
        for (GetandSet g : linknmed) {
            ch[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkedclickListenered.onCheckedClick(itemView, getAdapterPosition());
                }
            });
            i++;
        }
    }

    public void checkedclickListener(Group.ItemCheckedClickListenered itemCheckedClickListeners) {
        this.checkedclickListenered = itemCheckedClickListeners;
    }
}
}




Aucun commentaire:

Enregistrer un commentaire