1

ich gestresst Suche nach einer geeigneten Lösung für den Zustand der Radiobuttons in der Gruppe in ListView beim Scrollen durch die Fragen zu erhalten. Wann immer ich ein Optionsfeld auswähle und durch die Liste blättern, werden Optionsfelder zufällig ausgewählt.RadioGroup Optionsfelder ändern Status in ArrayAdapter ListView

public class QuizAdapter extends ArrayAdapter<Questions>{ 

      public QuizAdapter(Context context, ArrayList<Questions> QuestionList){ 

       super(context,0,QuestionList); 
      } 

      @NonNull 
      @Override 
      public View getView(final int position, View convertView, ViewGroup parent) { 
       View QuesView = convertView; 
       if(QuesView == null){ 
        QuesView = LayoutInflater.from(getContext()).inflate(R.layout.listlayout,parent,false); 
       } 
       final Questions tempQues = getItem(position); 

       TextView question = (TextView) QuesView.findViewById(R.id.ques); 
       TextView opt1 = (TextView) QuesView.findViewById(R.id.opt1); 
       TextView opt2 = (TextView) QuesView.findViewById(R.id.opt2); 
       TextView opt3 = (TextView) QuesView.findViewById(R.id.opt3); 
       TextView opt4 = (TextView) QuesView.findViewById(R.id.opt4);; 
       RadioGroup rg = (RadioGroup) QuesView.findViewById(R.id.rg); 
       question.setText(tempQues.getQuestion()); 
       opt1.setText(tempQues.getOpt1()); 
       opt2.setText(tempQues.getOpt2()); 
       opt3.setText(tempQues.getOpt3()); 
       opt4.setText(tempQues.getOpt4()); 

       rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
        @Override 
        public void onCheckedChanged(RadioGroup group, int checkedId) { 

         int radioButtonID = group.getCheckedRadioButtonId(); 
         RadioButton radioButton = (RadioButton) group.findViewById(radioButtonID); 
         Questions question = getItem(position); 
     //for checking current answer and correct answer 
          Log.v("QuizAdapter",radioButton.getText().toString()+" "+question.getAns()); 
          notifyDataSetChanged(); 
         } 
        }); 


        return QuesView; 
       }} 

ListLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="20dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Question?" 
     android:id="@+id/ques" 
     android:textStyle="bold" 
     android:textAppearance="?android:textAppearanceMedium"/> 

    <RadioGroup 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:paddingTop="8dp" 
     android:id="@+id/rg"> 


     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:hint="option 1" 
      android:id="@+id/opt1" /> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:hint="option 2" 
      android:id="@+id/opt2" /> 


     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:hint="option 3" 
      android:id="@+id/opt3" /> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:hint="option 4" 
      android:id="@+id/opt4" /> 
    </RadioGroup> 

</LinearLayout> 

Frage Klasse

public class Questions { 

     private String question; 
     private String opt1; 
     private String opt2; 
     private String opt3; 
     private String opt4; 
     private String ans; 

     public Questions(String question, String opt1, String opt2, String opt3, String opt4, String ans) { 
      this.question = question; 
      this.opt1 = opt1; 
      this.opt2 = opt2; 
      this.opt3 = opt3; 
      this.opt4 = opt4; 
      this.ans = ans; 
     } 

     public String getQuestion() { 
      return question; 
     } 

     public String getOpt1() { 
      return opt1; 
     } 

     public String getOpt2() { 
      return opt2; 
     } 

     public String getOpt3() { 
      return opt3; 
     } 

     public String getOpt4() { 
      return opt4; 
     } 

     public String getAns() { 
      return ans; 
     } 
     } 

ComputerQuiz Klasse

public class ComputerQuiz extends AppCompatActivity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_computer_quiz); 

      ArrayList<Questions> computerQuestionsList = new ArrayList<>(); 

      computerQuestionsList.add(new Questions("You should save your computer from","Viruses","Time bombs","Worms", "" + 
        "All of the above","All of the above")); 

      computerQuestionsList.add(new Questions("A light sensitive device that converts drawing, printed text or other " + 
        "images into digital form is","Keyboard","Scanner","MRE","Mouse","Scanner")); 

      computerQuestionsList.add(new Questions("The basic architecture of computer was developed by","John Von Neumann", 
        "Charles Babbage","Blaise Pascal","Garden Moore","John Von Neumann")); 

      computerQuestionsList.add(new Questions("In how many generations a computer can be classified?", 
        "3","4","5","6","5")); 

      computerQuestionsList.add(new Questions("Fifth generation computers are based on", 
        "Programming Intelligence","System Knowledge","Holograms","Artificial Intelligence","Artificial Intelligence")); 

      QuizAdapter ComQuestionList = new QuizAdapter(this,computerQuestionsList); 

      final ListView comList = (ListView) findViewById(R.id.comList); 
      comList.setAdapter(ComQuestionList); 

      View footer = LayoutInflater.from(getBaseContext()).inflate(R.layout.footer_button,null,false); 
      comList.addFooterView(footer); 

     } 

     } 

Antwort

0

Dieses Problem ist vor allem wegen des Adapter-Objekt wiederzuverwenden.

Also, was Sie tun sollten, sollten Sie das ausgewählte Element einige wie speichern und Sie sollten überprüfen und aktualisieren, wann immer es getView Methode in der Ansicht aufgerufen.

Beispiel: Wenn Sie die 2. Frage als B ausgewählt haben und auf Ihrem Bildschirm haben Sie nur 5 Fragen und Antworten. Wenn Sie mehr scrollen, wird Adapter versuchen, das alte Objekt wiederzuverwenden, möglicherweise wird Ihre achte Frage versuchen, das Objekt Ihrer zweiten Frage wiederzuverwenden, die bereits beantwortet hat.

Für Ihren Fall: Sie können erstellen HashMap<String, String> in Ihrem adapter wenn jemals Benutzer die Frage ausgewählt Sie die HashMap aktualisieren können. und innerhalb getView überprüfen Sie jede Frage, wenn bereits vorhanden, dann setzen Sie die Radio-Taste ausgewählt und andere Taste nicht ausgewählt.

+0

Vielen Dank für Ihre Antwort, aber können Sie bitte ein Beispiel geben? – user3828413

Verwandte Themen