0

i have the following code. If the name of the checkbox is in the 'deneme' array that the function takes, I want the checkbox to be checked. but those that don't enter the 'if' are marked strangely. and there are random checked when scrolling up and down

public SubjectRecyclerViewAdapter(ArrayList<Subject> subjects, ArrayList<String> deneme) {
    this.subjects = subjects;
    this.deneme = deneme;
}



    @Override
public void onBindViewHolder(@NonNull  SubjectRecyclerViewAdapter.MyViewHolder holder, int position) {

    holder.checkBox.setText(subjects.get(position).getCheckBoxSubject());

    if (deneme.contains(subjects.get(position).getCheckBoxSubject())) {
    
        holder.checkBox.setChecked(true);
    }}

1 Answers1

0

Because the ViewHolders are recycled (meaning they are reused as you scroll) you need to explicitly set the checkbox's state either way (so that it changes whatever previous state the ViewHolder had):

if (deneme.contains(subjects.get(position).getCheckBoxSubject())) {    
    holder.checkBox.setChecked(true);
} else {
    holder.checkBox.setChecked(false);
}
samaitch
  • 1,172
  • 7
  • 9