radiogroup未给出确切的结果

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

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:weightSum="2"
            android:id="@+id/newlinear1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_weight="1"
                android:id="@+id/question"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/question"
                android:textColor="@color/black"
                android:textAllCaps="true"
                android:textAlignment="center"
                android:textStyle="bold">

            </TextView>

            <ImageView
                android:layout_gravity="center"
                android:visibility="gone"
                android:src="@mipmap/ic_launcher_round"
                android:layout_weight="1"
                android:id="@+id/QuestionImage"
                android:layout_width="match_parent"
                android:layout_height="300dp">

            </ImageView>

        </LinearLayout>

        <LinearLayout
            android:background="@drawable/linear"
            android:id="@+id/linear2"
            android:layout_marginTop="10dp"
            android:layout_below="@id/newlinear1"
            android:layout_width="match_parent"
            android:layout_margin="40dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center">

            <RadioGroup
                android:id="@+id/radioPostGroup"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <RadioButton
                    android:layout_marginTop="25dp"
                    android:textColor="@color/black"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/answer1"
                    android:id="@+id/radioAnswer1">

                </RadioButton>
                <RadioButton
                    android:layout_marginTop="25dp"
                    android:textColor="@color/black"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/answer2"
                    android:id="@+id/radioAnswer2">

                </RadioButton>
                <RadioButton
                    android:layout_marginTop="25dp"
                    android:textColor="@color/black"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/answer3"
                    android:id="@+id/radioAnswer3">

                </RadioButton>
                <RadioButton
                    android:layout_marginTop="25dp"
                    android:textColor="@color/black"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/answer4"
                    android:id="@+id/radioAnswer4">

                </RadioButton>

            </RadioGroup>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/linear3"
            android:layout_below="@id/linear2"
            android:orientation="horizontal"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/submitAnswer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Submit">

            </Button>
<!--            <View-->
<!--                -->
<!--                android:layout_width="fill_parent"-->
<!--                android:layout_weight="2"-->
<!--                android:layout_height="10dp"-->
<!--                android:layout_marginTop="0.5dp"-->
<!--                android:background="@color/purple"/>-->

        </LinearLayout>

        <LinearLayout
            android:id="@+id/mylinear4"
            android:layout_below="@id/linear3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <com.like.LikeButton
                android:layout_weight="1"
                app:icon_type="heart"
                app:icon_size="10dp"
                android:id="@+id/myheart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            </com.like.LikeButton>

            <com.like.LikeButton
                android:layout_weight="1"
                app:icon_type="thumb"
                app:icon_size="10dp"
                android:id="@+id/mythumb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            </com.like.LikeButton>

            <com.like.LikeButton
                android:layout_weight="1"
                app:icon_type="star"
                app:icon_size="10dp"
                android:id="@+id/myStar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            </com.like.LikeButton>

        </LinearLayout>
        <View
            android:layout_below="@+id/mylinear4"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black">

        </View>

    </RelativeLayout>

    </RelativeLayout>

我的回收视图:

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<finalAdapter.MyHolder> {
    private List<QuestionData> dataset;
    private Context context;
    private DatabaseReference heartReference;
    private int counter = 0;
    boolean testCick = false;
    private RadioButton radioButton;
    private String CorrectAnswer;

    public finalAdapter(List<QuestionData> 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("userid", "onBindViewHolder: "+ userID);

        String posKey = dataset.get(position).getKey();

//        final FirebaseDatabase mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("likes");

//        final DatabaseReference databaseReference = mFirebaseDatabase.getReference("Questions").child(dataset.get(position).getGroup()).child(dataset.get(position).getKey());
        holder.getheartButtonStatus(posKey,userID);

        DatabaseReference likesReference = FirebaseDatabase.getInstance().getReference("Likes");

//        HashMap<String, Object> result = new HashMap<>();

        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("selected", "onClick: "+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("#9ded9f"));
                    }else{
                        radioButton.setBackgroundColor(Color.parseColor("#990e0e"));
                    }

                }
            });

        }

        public void getheartButtonStatus(String posKey, String userID) {
            heartReference = FirebaseDatabase.getInstance().getReference("Likes");
            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) {

                }
            });

        }
    }

}
enyaitl3

enyaitl31#

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

String correctAnswer;

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

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

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

if(radioButton.getText().equals(correctAnswer)){
    radioButton.setBackgroundColor(Color.parseColor("#9ded9f"));
} else {
    radioButton.setBackgroundColor(Color.parseColor("#990e0e"));
                }

相关问题