title: RadioGroup in Recyclerview is not giving exact result
body: <p>I am creating a MCQ app using recylerview where I have questions, 4 radio button and submit buttons. My program also has correct answer when submit click radio button and click submit button and if answer is correct I change the correct radio button background with green and red for wrong answer.</p>
  <p>Now problem is that there are multiple questions with multiple submit buttons when I click first question correct radio button it changes its background red, same for all questions but except for last.</p>
  <p>For last questions it gives correct background for correct and incorrect answer</p>
  <p>Gif of my problem: <a href="https://ibb.co/LzQSVNz" rel="nofollow noreferrer">https://ibb.co/LzQSVNz</a></p>
  <p>In gif in first question ww2 is correct, but code changed its color to red and 2nd questions which is last is giving correct results.</p>
  <p>My layout code:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:layout_marginTop=&quot;10dp&quot;
      android:layout_width=&quot;match_parent&quot;
      android:background=&quot;@color/white&quot;
      android:layout_height=&quot;wrap_content&quot;&gt;
  
      &lt;RelativeLayout
          android:background=&quot;@color/white&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;&gt;
  
          &lt;LinearLayout
              android:weightSum=&quot;2&quot;
              android:id=&quot;@+id/newlinear1&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:orientation=&quot;vertical&quot;&gt;
  
              &lt;TextView
                  android:layout_weight=&quot;1&quot;
                  android:id=&quot;@+id/question&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:text=&quot;@string/question&quot;
                  android:textColor=&quot;@color/black&quot;
                  android:textAllCaps=&quot;true&quot;
                  android:textAlignment=&quot;center&quot;
                  android:textStyle=&quot;bold&quot;&gt;
  
              &lt;/TextView&gt;
  
              &lt;ImageView
                  android:layout_gravity=&quot;center&quot;
                  android:visibility=&quot;gone&quot;
                  android:src=&quot;@mipmap/ic_launcher_round&quot;
                  android:layout_weight=&quot;1&quot;
                  android:id=&quot;@+id/QuestionImage&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;300dp&quot;&gt;
  
              &lt;/ImageView&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:background=&quot;@drawable/linear&quot;
              android:id=&quot;@+id/linear2&quot;
              android:layout_marginTop=&quot;10dp&quot;
              android:layout_below=&quot;@id/newlinear1&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_margin=&quot;40dp&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:orientation=&quot;vertical&quot;
              android:gravity=&quot;center&quot;&gt;
  
              &lt;RadioGroup
                  android:id=&quot;@+id/radioPostGroup&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;&gt;
  
                  &lt;RadioButton
                      android:layout_marginTop=&quot;25dp&quot;
                      android:textColor=&quot;@color/black&quot;
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:text=&quot;@string/answer1&quot;
                      android:id=&quot;@+id/radioAnswer1&quot;&gt;
  
                  &lt;/RadioButton&gt;
                  &lt;RadioButton
                      android:layout_marginTop=&quot;25dp&quot;
                      android:textColor=&quot;@color/black&quot;
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:text=&quot;@string/answer2&quot;
                      android:id=&quot;@+id/radioAnswer2&quot;&gt;
  
                  &lt;/RadioButton&gt;
                  &lt;RadioButton
                      android:layout_marginTop=&quot;25dp&quot;
                      android:textColor=&quot;@color/black&quot;
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:text=&quot;@string/answer3&quot;
                      android:id=&quot;@+id/radioAnswer3&quot;&gt;
  
                  &lt;/RadioButton&gt;
                  &lt;RadioButton
                      android:layout_marginTop=&quot;25dp&quot;
                      android:textColor=&quot;@color/black&quot;
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:text=&quot;@string/answer4&quot;
                      android:id=&quot;@+id/radioAnswer4&quot;&gt;
  
                  &lt;/RadioButton&gt;
  
  
              &lt;/RadioGroup&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:id=&quot;@+id/linear3&quot;
              android:layout_below=&quot;@id/linear2&quot;
              android:orientation=&quot;horizontal&quot;
              android:layout_centerInParent=&quot;true&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;&gt;
  
              &lt;Button
                  android:id=&quot;@+id/submitAnswer&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:text=&quot;Submit&quot;&gt;
  
              &lt;/Button&gt;
  &lt;!--            &lt;View--&gt;
  &lt;!--                --&gt;
  &lt;!--                android:layout_width=&quot;fill_parent&quot;--&gt;
  &lt;!--                android:layout_weight=&quot;2&quot;--&gt;
  &lt;!--                android:layout_height=&quot;10dp&quot;--&gt;
  &lt;!--                android:layout_marginTop=&quot;0.5dp&quot;--&gt;
  &lt;!--                android:background=&quot;@color/purple&quot;/&gt;--&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:id=&quot;@+id/mylinear4&quot;
              android:layout_below=&quot;@id/linear3&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:orientation=&quot;horizontal&quot;
              &gt;
  
              &lt;com.like.LikeButton
                  android:layout_weight=&quot;1&quot;
                  app:icon_type=&quot;heart&quot;
                  app:icon_size=&quot;10dp&quot;
                  android:id=&quot;@+id/myheart&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;&gt;
  
              &lt;/com.like.LikeButton&gt;
  
              &lt;com.like.LikeButton
                  android:layout_weight=&quot;1&quot;
                  app:icon_type=&quot;thumb&quot;
                  app:icon_size=&quot;10dp&quot;
                  android:id=&quot;@+id/mythumb&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;&gt;
  
              &lt;/com.like.LikeButton&gt;
  
              &lt;com.like.LikeButton
                  android:layout_weight=&quot;1&quot;
                  app:icon_type=&quot;star&quot;
                  app:icon_size=&quot;10dp&quot;
                  android:id=&quot;@+id/myStar&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;&gt;
  
              &lt;/com.like.LikeButton&gt;
  
          &lt;/LinearLayout&gt;
          &lt;View
              android:layout_below=&quot;@+id/mylinear4&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;1dp&quot;
              android:background=&quot;@color/black&quot;&gt;
  
          &lt;/View&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;/RelativeLayout&gt;
  </code></pre>
  <p>my recyclerview:</p>
  <pre><code>package com.example.mcqapp.activities.Adapters;
  
  import android.content.Context;
  import android.graphics.Color;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.RadioButton;
  import android.widget.RadioGroup;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.RecyclerView;
  
  import com.bumptech.glide.Glide;
  import com.example.mcqapp.R;
  import com.example.mcqapp.activities.Models.RequestDataModel;
  import com.example.mcqapp.activities.Models.RetrieveModel;
  import com.example.mcqapp.activities.forms.QuestionData;
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.auth.FirebaseUser;
  import com.google.firebase.database.DataSnapshot;
  import com.google.firebase.database.DatabaseError;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.database.ValueEventListener;
  import com.like.LikeButton;
  import com.like.OnLikeListener;
  
  import org.jetbrains.annotations.NotNull;
  import org.w3c.dom.Text;
  
  import java.util.HashMap;
  import java.util.List;
  
  public class finalAdapter extends RecyclerView.Adapter&lt;finalAdapter.MyHolder&gt; {
      private List&lt;QuestionData&gt; dataset;
      private Context context;
      private DatabaseReference heartReference;
      private int counter = 0;
      boolean testCick = false;
      private RadioButton radioButton;
      private String CorrectAnswer;
  
  
      public finalAdapter(List&lt;QuestionData&gt; dataset,Context context){
          this.dataset = dataset;
          this.context = context;
  
      }
  
  
      @NotNull
      @Override
      public MyHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
          LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
          View view = layoutInflater.inflate(R.layout.recyclerquestions,parent,false);
          
          MyHolder myHolder = new MyHolder(view);
  
          return myHolder;
      }
  
      @Override
      public void onBindViewHolder(@NonNull @NotNull finalAdapter.MyHolder holder, int position) {
          holder.question.setText(dataset.get(position).getQuestion());
          holder.radioButton1.setText(dataset.get(position).getAnswer1());
          holder.radioButton2.setText(dataset.get(position).getAnswer2());
          holder.radioButton3.setText(dataset.get(position).getAnswer3());
          holder.radioButton4.setText(dataset.get(position).getAnswer4());
  
          CorrectAnswer  = dataset.get(position).getCorrectAnswer();
  
          String im = dataset.get(position).getImageLink();
          if (im != null){
              holder.imageView.setVisibility(View.VISIBLE);
              Glide.with(context).load(dataset.get(position).getImageLink()).into(holder.imageView);
          }
  
          FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
          String userID = firebaseUser.getUid();
          Log.d(&quot;userid&quot;, &quot;onBindViewHolder: &quot;+ userID);
  
          String posKey = dataset.get(position).getKey();
  
  //        final FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance().getReference(&quot;likes&quot;);
  
  //        final DatabaseReference databaseReference = mFirebaseDatabase.getReference(&quot;Questions&quot;).child(dataset.get(position).getGroup()).child(dataset.get(position).getKey());
          holder.getheartButtonStatus(posKey,userID);
  
          DatabaseReference likesReference = FirebaseDatabase.getInstance().getReference(&quot;Likes&quot;);
  
  //        HashMap&lt;String, Object&gt; result = new HashMap&lt;&gt;();
  
          holder.heart.setOnLikeListener(new OnLikeListener() {
              @Override
              public void liked(LikeButton likeButton) {
                  testCick = true;
                  likesReference.addValueEventListener(new ValueEventListener() {
                      @Override
                      public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
                          if (testCick==true){
                              if (snapshot.child(posKey).hasChild(userID)){
  //                                likesReference.child(posKey).removeValue();
  //                                holder.heart.setLiked(true);
                                  testCick =false;
  
                              }else{
                                  likesReference.child(posKey).child(userID).setValue(true);
                                  testCick = false;
                              }
                          }
                      }
  
                      @Override
                      public void onCancelled(@NonNull @NotNull DatabaseError error) {
  
                      }
                  });
  
  
  
              }
  
              @Override
              public void unLiked(LikeButton likeButton) {
                  likesReference.child(posKey).removeValue();
                  testCick =false;
              }
          });
  
  
      }
  
      @Override
      public int getItemCount() {
          return dataset.size();
      }
  
      public class MyHolder extends RecyclerView.ViewHolder{
          TextView question;
          RadioButton radioButton1,radioButton2,radioButton3,radioButton4;
          ImageView imageView;
          LikeButton heart,thumb,star;
          RadioGroup radioGroup;
  
          private Button Button;
          public  MyHolder(View itemView){
              super(itemView);
              radioGroup =  itemView.findViewById(R.id.radioPostGroup);
              question = (TextView) itemView.findViewById(R.id.question);
              radioButton1 = itemView.findViewById(R.id.radioAnswer1);
              radioButton2 = itemView.findViewById(R.id.radioAnswer2);
              radioButton3 = itemView.findViewById(R.id.radioAnswer3);
              radioButton4 = itemView.findViewById(R.id.radioAnswer4);
              imageView = itemView.findViewById(R.id.QuestionImage);
              Button = itemView.findViewById(R.id.submitAnswer);
              heart= itemView.findViewById(R.id.myheart);
              thumb= itemView.findViewById(R.id.mythumb);
              star= itemView.findViewById(R.id.myStar);
  
              Button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      int selectedId = radioGroup.getCheckedRadioButtonId();
                      Log.d(&quot;selected&quot;, &quot;onClick: &quot;+selectedId);
                      radioButton = itemView.findViewById(selectedId);
            //**`problem is in this section`**
  
                      Toast.makeText(context.getApplicationContext(),radioButton.getText(),Toast.LENGTH_LONG).show();
  
                      if(radioButton.getText().equals(CorrectAnswer)){
                          radioButton.setBackgroundColor(Color.parseColor(&quot;#9ded9f&quot;));
                      }else{
                          radioButton.setBackgroundColor(Color.parseColor(&quot;#990e0e&quot;));
                      }
  
  
                  }
              });
  
          }
  
          public void getheartButtonStatus(String posKey, String userID) {
              heartReference = FirebaseDatabase.getInstance().getReference(&quot;Likes&quot;);
              heartReference.addValueEventListener(new ValueEventListener() {
                  @Override
                  public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
                      if (snapshot.child(posKey).hasChild(userID)){
                          int likeCount = (int)snapshot.child(posKey).getChildrenCount();
                          heart.setLiked(true);
                      }
                  }
  
                  @Override
                  public void onCancelled(@NonNull @NotNull DatabaseError error) {
  
                  }
              });
  
  
          }
      }
  
  }
  </code></pre>
  <h2>Already read</h2>
  <p><a href="https://stackoverflow.com/questions/33476951/radiogroup-in-recyclerview">Radiogroup in recyclerview</a></p>
  <p><a href="https://stackoverflow.com/questions/10460092/display-radiogroup-in-listview">Display RadioGroup in ListView</a></p>
  <p><a href="https://stackoverflow.com/questions/38977913/radiogroup-in-recyclerview-incorrect">Radiogroup in recyclerview incorrect</a></p>