Я отображаю набор вопросов mcq в представлении переработчика. В группе радиостанций есть 4 переключателя для 4 вариантов mcq. Максимальные переключатели в ячейках mcq работают нормально, но есть несколько переключателей, где я не могу проверить переключатель. Я видел, что они проверяются после нажатия, но просто пользовательский интерфейс не отображается.
Я видел, что, если я нажимаю другую радиокнопку, а затем первую нужную радиокнопку, она показывает нормально.
Предоставление следующих кодов:
Класс адаптера
package com.onssoftware.bcs.activity.exam;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import com.onssoftware.bcs.R;
import com.onssoftware.bcs.model.Exam;
import com.onssoftware.bcs.model.MCQ;
import java.util.List;
import androidx.recyclerview.widget.RecyclerView;
public class ExamAdapter extends RecyclerView.Adapter<ExamRow> {
private Exam exam;
private ExamActivity activity;
// Provide a suitable constructor (depends on the kind of dataset)
public ExamAdapter(Exam exam, ExamActivity activity) {
this.exam = exam;
this.activity = activity;
}
// Create new views (invoked by the layout manager)
@Override
public ExamRow onCreateViewHolder(ViewGroup parent,
int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.mcq_row_for_exam, parent, false);
// create a new view
ExamRow row = new ExamRow(view);
return row;
}
@Override
public int getItemCount() {
return exam.getMcqList().size();
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ExamRow row, final int position) {
// - get element from your data set at this position
// - replace the contents of the view with that element
//First clear previous selections
clearPreviousSelection(row);
final MCQ mcq = exam.getMcqList().get(position);
row.mcqDescriptionTextView.setText(mcq.getQuestionDescription());
List<String> options = mcq.getAllOptions();
row.option1.setText(options.get(0));
row.option2.setText(options.get(1));
row.option3.setText(options.get(2));
row.option4.setText(options.get(3));
int mcqIndex = options.indexOf(mcq.getStudentAnswer());
if (mcqIndex != -1) {
if (mcqIndex == 0) {
row.option1.setChecked(true);
}
else if (mcqIndex == 1) {
row.option2.setChecked(true);
}
else if (mcqIndex == 2) {
row.option3.setChecked(true);
}
else if (mcqIndex == 3) {
row.option4.setChecked(true);
}
}
// View.OnClickListener l = new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// onRadioButtonClicked(v, mcq);
// }
// };
row.option1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
RadioButton radioButton = (RadioButton) buttonView;
String ans = radioButton.getText().toString();
mcq.setStudentAnswer(ans);
notifyDataSetChanged();
}
else {
//mcq.setStudentAnswer("");
}
}
});
row.option2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
RadioButton radioButton = (RadioButton) buttonView;
String ans = radioButton.getText().toString();
mcq.setStudentAnswer(ans);
notifyDataSetChanged();
}
else {
//mcq.setStudentAnswer("");
}
}
});
row.option3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
RadioButton radioButton = (RadioButton) buttonView;
String ans = radioButton.getText().toString();
mcq.setStudentAnswer(ans);
notifyDataSetChanged();
}
else {
//mcq.setStudentAnswer("");
}
}
});
row.option4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
RadioButton radioButton = (RadioButton) buttonView;
String ans = radioButton.getText().toString();
mcq.setStudentAnswer(ans);
notifyDataSetChanged();
}
else {
//mcq.setStudentAnswer("");
}
}
});
/*row.option1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRadioButtonClicked(v, mcq);
}
});
row.option2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRadioButtonClicked(v, mcq);
}
});
row.option3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRadioButtonClicked(v, mcq);
}
});
row.option4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRadioButtonClicked(v, mcq);
}
});*/
}
private void clearPreviousSelection(ExamRow row) {
clearPreviousSelection(row.option1);
clearPreviousSelection(row.option2);
clearPreviousSelection(row.option3);
clearPreviousSelection(row.option4);
}
private void clearPreviousSelection(RadioButton option) {
option.setOnCheckedChangeListener(null);
option.setChecked(false);
// option.setOnClickListener(null);
}
public void onRadioButtonClicked(View view, MCQ mcq) {
// Is the button now checked?
RadioButton radioButton = (RadioButton) view;
if (radioButton.isChecked()) {
mcq.setStudentAnswer(radioButton.getText().toString());
}
else {
mcq.setStudentAnswer("");
}
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
}
Ячейка xml код:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cv"
style="@style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
tools:ignore="ContentDescription">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/mcqDescriptionTextView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="mcqDescriptionTextView"
android:textColor="#3F51B5"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mcqDescriptionTextView"
app:layout_constraintVertical_bias="0.0">
<RadioButton
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
tools:layout_editor_absoluteX="61dp"
tools:layout_editor_absoluteY="99dp" />
<RadioButton
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
tools:layout_editor_absoluteX="61dp"
tools:layout_editor_absoluteY="99dp" />
<RadioButton
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
tools:layout_editor_absoluteX="61dp"
tools:layout_editor_absoluteY="99dp" />
<RadioButton
android:id="@+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
tools:layout_editor_absoluteX="61dp"
tools:layout_editor_absoluteY="99dp" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Проверьте, есть ли у вас какие-либо идеи. Спасибо.