Hello i have an option in my recycler view that contains a checkbox and it works but the problem is when i scroll another one random gets selected and i don't want this, i need to select some of them but not random, i don't know why this happens. My adapter for that options is this, i get the selected items once they get charged in my recycler view.
public class OpcionesAdapter extends RecyclerView.Adapter<OpcionesAdapter.ViewHolderOpciones> {
Context mCtx;
ArrayList<OpcionesClass> opcionesList;
public ArrayList<OpcionesClass> checkedOptions = new ArrayList<>();
public OpcionesAdapter(Context mCtx, ArrayList<OpcionesClass> opcionesList) {
this.mCtx = mCtx;
this.opcionesList = opcionesList;
}
@NonNull
@Override
public ViewHolderOpciones onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemopcion, null);
return new ViewHolderOpciones(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolderOpciones holder, int position) {
holder.tituloOpcion.setText(opcionesList.get(position).getTituloOpcion());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(View view, int pos) {
CheckBox chk = (CheckBox) view;
if (chk.isChecked()) {
checkedOptions.add(opcionesList.get(pos));
} else if (!chk.isChecked()) {
checkedOptions.remove(opcionesList.get(pos));
}
}
});
}
@Override
public int getItemCount() {
return opcionesList.size();
}
public class ViewHolderOpciones extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tituloOpcion;
CheckBox chkOption;
ItemClickListener itemClickListener;
public ViewHolderOpciones(@NonNull View itemView) {
super(itemView);
tituloOpcion = itemView.findViewById(R.id.tv_TituloOpcion);
chkOption = itemView.findViewById(R.id.chck_opcion);
chkOption.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener ic) {
this.itemClickListener = ic;
}
@Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v, getLayoutPosition());
}
}
interface ItemClickListener {
void onItemClick(View view, int pos);
}
}
Aucun commentaire:
Enregistrer un commentaire