Для контекста этот recyclerView предназначен для вопроса с множественным выбором, а recyclerView отображает варианты, которые пользователь может выбрать для ответа на вопрос, этот вопрос с множественным выбором может иметь другое количество требуемых опций.
Это это адаптер прямо сейчас?
public class CheckboxAdapter extends RecyclerView.Adapter<CheckboxAdapter.CheckboxViewHolder> {
private static List<SurveyQuestionOptions> mOptions;
private static List<SurveyQuestions> mQuestions;
private static SparseBooleanArray itemsState = new SparseBooleanArray();
public static class CheckboxViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener
{
CheckBox checkBox;
public CheckboxViewHolder(@NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.checkbox);
}
void bind(int position) {
if (!itemsState.get(position, false)) {
checkBox.setChecked(false);
} else {
checkBox.setChecked(true);
}
checkBox.setText(mOptions.get(position).getOption());
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
SurveyQuestionOptions currentOptions = mOptions.get(position);
Optional<SurveyQuestions> currentQuestion = mQuestions.stream()
.filter(c -> c.getId().equals(currentOptions.getSurveyQuestionId())).findFirst();
//none of this system prints appear in the console logcat
if (itemsState.size() <= currentQuestion.get().getNum()) {
System.out.println("first if");
if (!itemsState.get(position, false)) {
checkBox.setChecked(true);
itemsState.put(position, true);
System.out.println("checked");
} else {
checkBox.setChecked(false);
itemsState.put(position, false);
System.out.println("unchecked");
}
}
}
}
public CheckboxAdapter(List<SurveyQuestionOptions> optionsList, List<SurveyQuestions> questionsList) {
mOptions = optionsList;
mQuestions = questionsList;
}
@NonNull
@Override
public CheckboxViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_survey_question_multichoice_checkboxes, parent, false);
return new CheckboxViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CheckboxViewHolder holder, int position) {
holder.bind(position);
}
@Override
public int getItemCount() {
if (mOptions == null) {
return 0;
} else {
return mOptions.size();
}
}
}
Как я могу исправить это onClick, чтобы при достижении количества требуемых параметров остальные флажки становились недоступными?