У меня есть пользовательский адаптер списка, который имеет вопрос в текстовом режиме и четыре варианта ответа в Radio Buttons. Все радио кнопки соединены вместе в Radio Group. Проблема в том, что я не могу отслеживать проверенные кнопки. При прокрутке кнопки проверяются случайным образом. Как я могу хранить переключатели, которые выбраны для конкретногоКак использовать Radiogroup с 4 различными кнопками радио в ArrayAdapter
общественного класса MCQAdapter расширяет ArrayAdapter {
public MCQAdapter(Activity context, ArrayList<MCQ> mcq) {
super(context, 0, mcq);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_mcqlist, parent, false);
}
final MCQ currentContent = (MCQ) getItem(position);
TextView content = (TextView) listItemView.findViewById(R.id.Question_Block);
content.setText(currentContent.getQuestion());
RadioGroup rg = (RadioGroup) listItemView.findViewById(R.id.Radio_Group);
final RadioButton rb1 = (RadioButton) listItemView.findViewById(R.id.Option1_Block);
rb1.setText(currentContent.getOptionA());
final RadioButton rb2 = (RadioButton) listItemView.findViewById(R.id.Option2_Block);
rb2.setText(currentContent.getOptionB());
final RadioButton rb3 = (RadioButton) listItemView.findViewById(R.id.Option3_Block);
rb3.setText(currentContent.getOptionC());
final RadioButton rb4 = (RadioButton) listItemView.findViewById(R.id.Option4_Block);
rb4.setText(currentContent.getOptionD());
return listItemView;
}
}
public class MCQ {
String question,optionA,optionB,optionC,optionD,answer,userAnswer;
public MCQ(String quest,String a,String b,String c, String d,String ans){
question = quest;
optionA = a;
optionB = b;
optionC = c;
optionD = d;
answer = ans;
}
public String getQuestion(){
return question;
}
public String getOptionA() {
return optionA;
}
public String getOptionB() {
return optionB;
}
public String getOptionC() {
return optionC;
}
public String getOptionD() {
return optionD;
}
public String getAnswer() {
return answer;
}
public String getUserAnswer() {
return userAnswer;
}
public void setUserAnswer(String userAnswer) {
this.userAnswer = userAnswer;
}
}
public class PropertiesOfConstructionMaterail extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_properties_of_construction_materail);
final ArrayList<MCQ> mcq = new ArrayList<MCQ>();
//Creating Different Instance Of MCQ to genarte various questions for Properties of Consrtuction Material
mcq.add(new MCQ("The property of material by which it can be beaten or rolled into thin plates, is called ?",
"Malleability","Ductility","Plasticity","Elasticity","Malleability"));
mcq.add(new MCQ("The property by which a body returns to its original shape after removal of the force, is called ?",
"Plasticity","Elasticity","Ductility","Malleability","Elasticity"));
mcq.add(new MCQ("The property of a material by which it can be drawn into smaller section due to tension, is called ?"
,"Plasticity","Ductility","Elasticity","Malleability","Ductility"));
MCQAdapter adapter = new MCQAdapter(this,mcq);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_mcqlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.silen.civilengineeringmcq.MCQList"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Question_Block"
android:textSize="18dp"
android:textStyle="bold"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Radio_Group">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Option1_Block"
android:checked="false"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Option2_Block"
android:checked="false"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Option3_Block"
android:checked="false"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Option4_Block"
android:checked="false"/>
</RadioGroup>
Я все еще не могу заставить код работать. Я сделал так, как вы меня спрашивали, но когда я проверяю опцию для первого радиокамера и прокручиваю вниз, первоначальная проверка исчезает и переносится в какую-то другую радиогруппу –
@NikhilRauniyar в представлениях ListView элементов перерабатываются списком, т. Е. Создается вид один раз, а затем передается как следующий элемент, когда предыдущий подключенный элемент становится невидимым. Поэтому, если вы не сбросили состояние должным образом в представлении 'getView', будут представлены некоторые из предыдущих элементов. Так как я предложил вам выполнить этот 'rb1.setChecked (viewModel.getRadioButtonSelectedId() == R.id.Option1_Block)' для каждого вашего переключателя (rb1,2,3,4), а также инициализировать ViewModel с помощью первого радио идентификатор кнопки. См. Отредактированный ответ – Beloo
. Можете ли вы привести пример кода или пример, который напоминает мою проблему, даже после того, как я реализую свой метод, у меня все еще есть та же проблема. Я застрял в этом вопросе более одного дня –