radiogroup未给出确切的结果

ut6juiuv  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(305)

已阅读:
recyclerview中的radiogroup
在listview中显示radiogroup
recyclerview中的radiogroup不正确
你好
我正在使用recylerview创建一个mcq应用程序,其中我有问题、4个单选按钮和提交按钮。我的程序在提交时也有正确的应答。单击单选按钮并单击提交按钮,如果答案正确,我会将正确的单选按钮背景更改为绿色和红色,表示回答错误
现在的问题是,有多个问题和多个提交按钮,当我点击第一个问题正确单选按钮时,它会改变其背景红色,所有问题相同,但最后一个除外。
对于最后的问题,它给出了正确和错误答案的正确背景
关于我的问题:https://ibb.co/lzqsvnz 在gif中,第一个问题ww2是正确的,但代码将其颜色更改为红色,第二个问题是最后一个问题给出了正确的结果
我的布局代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_marginTop="10dp"
  5. android:layout_width="match_parent"
  6. android:background="@color/white"
  7. android:layout_height="wrap_content">
  8. <RelativeLayout
  9. android:background="@color/white"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <LinearLayout
  13. android:weightSum="2"
  14. android:id="@+id/newlinear1"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:orientation="vertical">
  18. <TextView
  19. android:layout_weight="1"
  20. android:id="@+id/question"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:text="@string/question"
  24. android:textColor="@color/black"
  25. android:textAllCaps="true"
  26. android:textAlignment="center"
  27. android:textStyle="bold">
  28. </TextView>
  29. <ImageView
  30. android:layout_gravity="center"
  31. android:visibility="gone"
  32. android:src="@mipmap/ic_launcher_round"
  33. android:layout_weight="1"
  34. android:id="@+id/QuestionImage"
  35. android:layout_width="match_parent"
  36. android:layout_height="300dp">
  37. </ImageView>
  38. </LinearLayout>
  39. <LinearLayout
  40. android:background="@drawable/linear"
  41. android:id="@+id/linear2"
  42. android:layout_marginTop="10dp"
  43. android:layout_below="@id/newlinear1"
  44. android:layout_width="match_parent"
  45. android:layout_margin="40dp"
  46. android:layout_height="wrap_content"
  47. android:orientation="vertical"
  48. android:gravity="center">
  49. <RadioGroup
  50. android:id="@+id/radioPostGroup"
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content">
  53. <RadioButton
  54. android:layout_marginTop="25dp"
  55. android:textColor="@color/black"
  56. android:layout_width="match_parent"
  57. android:layout_height="wrap_content"
  58. android:text="@string/answer1"
  59. android:id="@+id/radioAnswer1">
  60. </RadioButton>
  61. <RadioButton
  62. android:layout_marginTop="25dp"
  63. android:textColor="@color/black"
  64. android:layout_width="match_parent"
  65. android:layout_height="wrap_content"
  66. android:text="@string/answer2"
  67. android:id="@+id/radioAnswer2">
  68. </RadioButton>
  69. <RadioButton
  70. android:layout_marginTop="25dp"
  71. android:textColor="@color/black"
  72. android:layout_width="match_parent"
  73. android:layout_height="wrap_content"
  74. android:text="@string/answer3"
  75. android:id="@+id/radioAnswer3">
  76. </RadioButton>
  77. <RadioButton
  78. android:layout_marginTop="25dp"
  79. android:textColor="@color/black"
  80. android:layout_width="match_parent"
  81. android:layout_height="wrap_content"
  82. android:text="@string/answer4"
  83. android:id="@+id/radioAnswer4">
  84. </RadioButton>
  85. </RadioGroup>
  86. </LinearLayout>
  87. <LinearLayout
  88. android:id="@+id/linear3"
  89. android:layout_below="@id/linear2"
  90. android:orientation="horizontal"
  91. android:layout_centerInParent="true"
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content">
  94. <Button
  95. android:id="@+id/submitAnswer"
  96. android:layout_width="wrap_content"
  97. android:layout_height="wrap_content"
  98. android:text="Submit">
  99. </Button>
  100. <!-- <View-->
  101. <!-- -->
  102. <!-- android:layout_width="fill_parent"-->
  103. <!-- android:layout_weight="2"-->
  104. <!-- android:layout_height="10dp"-->
  105. <!-- android:layout_marginTop="0.5dp"-->
  106. <!-- android:background="@color/purple"/>-->
  107. </LinearLayout>
  108. <LinearLayout
  109. android:id="@+id/mylinear4"
  110. android:layout_below="@id/linear3"
  111. android:layout_width="match_parent"
  112. android:layout_height="wrap_content"
  113. android:orientation="horizontal"
  114. >
  115. <com.like.LikeButton
  116. android:layout_weight="1"
  117. app:icon_type="heart"
  118. app:icon_size="10dp"
  119. android:id="@+id/myheart"
  120. android:layout_width="wrap_content"
  121. android:layout_height="wrap_content">
  122. </com.like.LikeButton>
  123. <com.like.LikeButton
  124. android:layout_weight="1"
  125. app:icon_type="thumb"
  126. app:icon_size="10dp"
  127. android:id="@+id/mythumb"
  128. android:layout_width="wrap_content"
  129. android:layout_height="wrap_content">
  130. </com.like.LikeButton>
  131. <com.like.LikeButton
  132. android:layout_weight="1"
  133. app:icon_type="star"
  134. app:icon_size="10dp"
  135. android:id="@+id/myStar"
  136. android:layout_width="wrap_content"
  137. android:layout_height="wrap_content">
  138. </com.like.LikeButton>
  139. </LinearLayout>
  140. <View
  141. android:layout_below="@+id/mylinear4"
  142. android:layout_width="match_parent"
  143. android:layout_height="1dp"
  144. android:background="@color/black">
  145. </View>
  146. </RelativeLayout>
  147. </RelativeLayout>

我的回收视图:

  1. package com.example.mcqapp.activities.Adapters;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.util.Log;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. import android.widget.RadioButton;
  11. import android.widget.RadioGroup;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. import androidx.annotation.NonNull;
  15. import androidx.recyclerview.widget.RecyclerView;
  16. import com.bumptech.glide.Glide;
  17. import com.example.mcqapp.R;
  18. import com.example.mcqapp.activities.Models.RequestDataModel;
  19. import com.example.mcqapp.activities.Models.RetrieveModel;
  20. import com.example.mcqapp.activities.forms.QuestionData;
  21. import com.google.firebase.auth.FirebaseAuth;
  22. import com.google.firebase.auth.FirebaseUser;
  23. import com.google.firebase.database.DataSnapshot;
  24. import com.google.firebase.database.DatabaseError;
  25. import com.google.firebase.database.DatabaseReference;
  26. import com.google.firebase.database.FirebaseDatabase;
  27. import com.google.firebase.database.ValueEventListener;
  28. import com.like.LikeButton;
  29. import com.like.OnLikeListener;
  30. import org.jetbrains.annotations.NotNull;
  31. import org.w3c.dom.Text;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. public class finalAdapter extends RecyclerView.Adapter<finalAdapter.MyHolder> {
  35. private List<QuestionData> dataset;
  36. private Context context;
  37. private DatabaseReference heartReference;
  38. private int counter = 0;
  39. boolean testCick = false;
  40. private RadioButton radioButton;
  41. private String CorrectAnswer;
  42. public finalAdapter(List<QuestionData> dataset,Context context){
  43. this.dataset = dataset;
  44. this.context = context;
  45. }
  46. @NotNull
  47. @Override
  48. public MyHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
  49. LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
  50. View view = layoutInflater.inflate(R.layout.recyclerquestions,parent,false);
  51. MyHolder myHolder = new MyHolder(view);
  52. return myHolder;
  53. }
  54. @Override
  55. public void onBindViewHolder(@NonNull @NotNull finalAdapter.MyHolder holder, int position) {
  56. holder.question.setText(dataset.get(position).getQuestion());
  57. holder.radioButton1.setText(dataset.get(position).getAnswer1());
  58. holder.radioButton2.setText(dataset.get(position).getAnswer2());
  59. holder.radioButton3.setText(dataset.get(position).getAnswer3());
  60. holder.radioButton4.setText(dataset.get(position).getAnswer4());
  61. CorrectAnswer = dataset.get(position).getCorrectAnswer();
  62. String im = dataset.get(position).getImageLink();
  63. if (im != null){
  64. holder.imageView.setVisibility(View.VISIBLE);
  65. Glide.with(context).load(dataset.get(position).getImageLink()).into(holder.imageView);
  66. }
  67. FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
  68. String userID = firebaseUser.getUid();
  69. Log.d("userid", "onBindViewHolder: "+ userID);
  70. String posKey = dataset.get(position).getKey();
  71. // final FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("likes");
  72. // final DatabaseReference databaseReference = mFirebaseDatabase.getReference("Questions").child(dataset.get(position).getGroup()).child(dataset.get(position).getKey());
  73. holder.getheartButtonStatus(posKey,userID);
  74. DatabaseReference likesReference = FirebaseDatabase.getInstance().getReference("Likes");
  75. // HashMap<String, Object> result = new HashMap<>();
  76. holder.heart.setOnLikeListener(new OnLikeListener() {
  77. @Override
  78. public void liked(LikeButton likeButton) {
  79. testCick = true;
  80. likesReference.addValueEventListener(new ValueEventListener() {
  81. @Override
  82. public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
  83. if (testCick==true){
  84. if (snapshot.child(posKey).hasChild(userID)){
  85. // likesReference.child(posKey).removeValue();
  86. // holder.heart.setLiked(true);
  87. testCick =false;
  88. }else{
  89. likesReference.child(posKey).child(userID).setValue(true);
  90. testCick = false;
  91. }
  92. }
  93. }
  94. @Override
  95. public void onCancelled(@NonNull @NotNull DatabaseError error) {
  96. }
  97. });
  98. }
  99. @Override
  100. public void unLiked(LikeButton likeButton) {
  101. likesReference.child(posKey).removeValue();
  102. testCick =false;
  103. }
  104. });
  105. }
  106. @Override
  107. public int getItemCount() {
  108. return dataset.size();
  109. }
  110. public class MyHolder extends RecyclerView.ViewHolder{
  111. TextView question;
  112. RadioButton radioButton1,radioButton2,radioButton3,radioButton4;
  113. ImageView imageView;
  114. LikeButton heart,thumb,star;
  115. RadioGroup radioGroup;
  116. private Button Button;
  117. public MyHolder(View itemView){
  118. super(itemView);
  119. radioGroup = itemView.findViewById(R.id.radioPostGroup);
  120. question = (TextView) itemView.findViewById(R.id.question);
  121. radioButton1 = itemView.findViewById(R.id.radioAnswer1);
  122. radioButton2 = itemView.findViewById(R.id.radioAnswer2);
  123. radioButton3 = itemView.findViewById(R.id.radioAnswer3);
  124. radioButton4 = itemView.findViewById(R.id.radioAnswer4);
  125. imageView = itemView.findViewById(R.id.QuestionImage);
  126. Button = itemView.findViewById(R.id.submitAnswer);
  127. heart= itemView.findViewById(R.id.myheart);
  128. thumb= itemView.findViewById(R.id.mythumb);
  129. star= itemView.findViewById(R.id.myStar);
  130. Button.setOnClickListener(new View.OnClickListener() {
  131. @Override
  132. public void onClick(View view) {
  133. int selectedId = radioGroup.getCheckedRadioButtonId();
  134. Log.d("selected", "onClick: "+selectedId);
  135. radioButton = itemView.findViewById(selectedId);
  136. //**`problem is in this section`**
  137. Toast.makeText(context.getApplicationContext(),radioButton.getText(),Toast.LENGTH_LONG).show();
  138. if(radioButton.getText().equals(CorrectAnswer)){
  139. radioButton.setBackgroundColor(Color.parseColor("#9ded9f"));
  140. }else{
  141. radioButton.setBackgroundColor(Color.parseColor("#990e0e"));
  142. }
  143. }
  144. });
  145. }
  146. public void getheartButtonStatus(String posKey, String userID) {
  147. heartReference = FirebaseDatabase.getInstance().getReference("Likes");
  148. heartReference.addValueEventListener(new ValueEventListener() {
  149. @Override
  150. public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
  151. if (snapshot.child(posKey).hasChild(userID)){
  152. int likeCount = (int)snapshot.child(posKey).getChildrenCount();
  153. heart.setLiked(true);
  154. }
  155. }
  156. @Override
  157. public void onCancelled(@NonNull @NotNull DatabaseError error) {
  158. }
  159. });
  160. }
  161. }
  162. }
enyaitl3

enyaitl31#

每次将数据绑定到viewholder时,都会设置 CorrectAnswer 达到一定的价值。问题是只有一个 CorrectAnswer 它包含最后一个值绑定,可以是显示的任何内容,也可以是少数未显示的内容。
您可以通过使用viewholder中的变量将正确答案与每个viewholder关联,或根据位置从数组中获取答案来解决此问题。
例如,在myholder中,定义成员变量如下:

  1. String correctAnswer;

删除全局 CorrectAnswer 变量
然后,在 onBindViewHolder() 如下所示设置变量:

  1. holder.correctAnswer = dataset.get(position).getCorrectAnswer();

现在,在click listener中,您可以检查所问问题的正确答案。每个myholder都包含它所代表的问题的答案。

  1. if(radioButton.getText().equals(correctAnswer)){
  2. radioButton.setBackgroundColor(Color.parseColor("#9ded9f"));
  3. } else {
  4. radioButton.setBackgroundColor(Color.parseColor("#990e0e"));
  5. }
展开查看全部

相关问题