java - Android RecyclerView 项目选择问题

标签 java android android-recyclerview

我正在设计一个实时问答应用程序,它从服务器获取数据,问题显示在包含一个问题和四个选项的 RecyclerView 中。现在,当我为给定问题选择一个选项时,它会被正确选择,但与此同时,其他问题的相应选项也会自动选择。

项目选择问题的屏幕截图如下。

enter image description here

这是我的 RecyclerView 的适配器类

public class LiveTestAdapter extends RecyclerView.Adapter<LiveTestAdapter.CustomViewHolder>{
    private int mItemSelected=-1;
    private List<DmLiveQuiz> questionList;
    DmLiveQuiz questionsList; // DmLiveQuiz questionsList
    private Context context; //context
    final DataHolder dh=new DataHolder();
    public List<Integer> myResponse= new ArrayList<Integer>();
    public int qno;
    public String myQno;
    public int afterSub;
    DataHolder dataHolder;

    public LiveTestAdapter(List<DmLiveQuiz> questionList, Context context) {
        this.questionList = questionList;
        this.context = context; 
    }

    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.live_quiz_display_format,parent,false);
        return new CustomViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(@NonNull final CustomViewHolder holder,  int position) {

        questionsList=questionList.get(holder.getAdapterPosition());
        holder.tvQNo.setText(questionsList.getQuestionId()+"");
        holder.tvquestion.getLayoutParams().width= LinearLayout.LayoutParams.WRAP_CONTENT;
        holder.tvquestion.setText(questionsList.getQuestion());
        holder.optA.setText(questionsList.getOptA());
        holder.optB.setText(questionsList.getOptB());
        holder.optC.setText(questionsList.getOptC());
        holder.optD.setText(questionsList.getOptD());
        holder.optA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                holder.optA.setBackgroundResource(R.drawable.button_border);
                holder.optB.setBackgroundResource(R.drawable.button_question_style);
                holder.optC.setBackgroundResource(R.drawable.button_question_style);
                holder.optD.setBackgroundResource(R.drawable.button_question_style);
                Toast toast = Toast.makeText(context, "Position :"+holder.getAdapterPosition(), Toast.LENGTH_SHORT);
                toast.show();

            }
        });
        holder.optB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                holder.optA.setBackgroundResource(R.drawable.button_question_style);
                holder.optB.setBackgroundResource(R.drawable.button_border);
                holder.optC.setBackgroundResource(R.drawable.button_question_style);
                holder.optD.setBackgroundResource(R.drawable.button_question_style);
                Toast toast = Toast.makeText(context, "Position :"+holder.getAdapterPosition(), Toast.LENGTH_SHORT);
                toast.show();

            }
        });
        holder.optC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                holder.optA.setBackgroundResource(R.drawable.button_question_style);
                holder.optB.setBackgroundResource(R.drawable.button_question_style);
                holder.optC.setBackgroundResource(R.drawable.button_border);
                holder.optD.setBackgroundResource(R.drawable.button_question_style);
                Toast toast = Toast.makeText(context, "Position :"+holder.getAdapterPosition(), Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        holder.optD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                holder.optA.setBackgroundResource(R.drawable.button_question_style);
                holder.optB.setBackgroundResource(R.drawable.button_question_style);
                holder.optC.setBackgroundResource(R.drawable.button_question_style);
                holder.optD.setBackgroundResource(R.drawable.button_border);
                Toast toast = Toast.makeText(context, "Position :"+holder.getAdapterPosition(), Toast.LENGTH_SHORT);
                toast.show();

            }
        });
        holder.tvClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                holder.optA.setBackgroundResource(R.drawable.button_question_style);
                holder.optB.setBackgroundResource(R.drawable.button_question_style);
                holder.optC.setBackgroundResource(R.drawable.button_question_style);
                holder.optD.setBackgroundResource(R.drawable.button_question_style);

            }
        });

    }

    @Override
    public int getItemCount() {
        return questionList.size();
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder{
        TextView tvquestion, tvClear,tvQNo;
        Button optA,optB,optC,optD;
        public CustomViewHolder(View itemView) {
            super(itemView);
            tvQNo=(TextView)itemView.findViewById(R.id.tvLiveQuizQuestionNo);
            tvquestion=(TextView)itemView.findViewById(R.id.tvLiveQuizQuestion);
            optA=(Button)itemView.findViewById(R.id.buttonOptionA);
            optB=(Button)itemView.findViewById(R.id.buttonOptionB);
            optC=(Button)itemView.findViewById(R.id.buttonOptionC);
            optD=(Button)itemView.findViewById(R.id.buttonOptionD);
            tvClear=(TextView)itemView.findViewById(R.id.tvClearSelection);

        }
    } 
}

我面临的唯一问题是自动选择未回答的选项。

请帮助我只选择选中的选项,而不是未选中的选项。提前致谢。

最佳答案

这些 View 将在您的 RecyclerView 中重复使用,因此您遇到了这样的问题。在您的情况下,您可能会考虑使用另一个数组来存储测验的答案,并可以跟踪 RecyclerView 中的每个项目。

我想建议像下面这样修改您的适配器。我在一些地方发表了评论。希望能帮助您理解您的问题。

public class LiveTestAdapter extends RecyclerView.Adapter<LiveTestAdapter.CustomViewHolder> {

    private int mItemSelected = -1;
    private List<DmLiveQuiz> questionList;

    private int[] answerList; // Get a list of your answers here.

    private DmLiveQuiz questionsList;
    private Context context;
    final DataHolder dh = new DataHolder();
    public List<Integer> myResponse = new ArrayList<Integer>();
    public int qno;
    public String myQno;
    public int afterSub;
    DataHolder dataHolder;

    public LiveTestAdapter(List<DmLiveQuiz> questionList, Context context) {
        this.questionList = questionList;
        this.context = context;
    }

    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.live_quiz_display_format, parent, false);
        return new CustomViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final CustomViewHolder holder, int position) {

        questionsList = questionList.get(holder.getAdapterPosition());
        holder.tvQNo.setText(questionsList.getQuestionId() + "");
        holder.tvquestion.getLayoutParams().width = LinearLayout.LayoutParams.WRAP_CONTENT;
        holder.tvquestion.setText(questionsList.getQuestion());
        holder.optA.setText(questionsList.getOptA());
        holder.optB.setText(questionsList.getOptB());
        holder.optC.setText(questionsList.getOptC());
        holder.optD.setText(questionsList.getOptD());

        // Now you need to modify the backgrounds of your option buttons like the following.
        if (answerList[position] == 1) holder.optA.setBackgroundResource(R.drawable.button_border);
        else holder.optA.setBackgroundResource(R.drawable.button_question_style);

        if (answerList[position] == 2) holder.optB.setBackgroundResource(R.drawable.button_border);
        else holder.optB.setBackgroundResource(R.drawable.button_question_style);

        if (answerList[position] == 3) holder.optC.setBackgroundResource(R.drawable.button_border);
        else holder.optC.setBackgroundResource(R.drawable.button_question_style);

        if (answerList[position] == 4) holder.optD.setBackgroundResource(R.drawable.button_border);
        else holder.optD.setBackgroundResource(R.drawable.button_question_style);

        holder.optA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.optA.setBackgroundResource(R.drawable.button_border);
                answerList[position] = 1; // Selected first option which is A
                Toast.makeText(context, "Position :" + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.optB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.optB.setBackgroundResource(R.drawable.button_border);
                answerList[position] = 2; // Selected second option which is B
                Toast.makeText(context, "Position :" + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.optC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.optC.setBackgroundResource(R.drawable.button_border);
                answerList[position] = 3; // Selected third option which is C
                Toast.makeText(context, "Position :" + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.optD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.optD.setBackgroundResource(R.drawable.button_border);
                answerList[position] = 4; // Selected fourth option which is D
                Toast.makeText(context, "Position :" + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.tvClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.optA.setBackgroundResource(R.drawable.button_question_style);
                holder.optB.setBackgroundResource(R.drawable.button_question_style);
                holder.optC.setBackgroundResource(R.drawable.button_question_style);
                holder.optD.setBackgroundResource(R.drawable.button_question_style);
                answerList[position] = 0; // Clear the value in the answerList
            }
        });
    }

    // Use this function to set the question list in the adapter
    public void setQuestionList(List<DmLiveQuiz> questionList) {
        this.questionList = questionList;
        this.answerList = new int[questionList.size()]; // This initializes the answer list having the same size as the question list
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return questionList.size();
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        TextView tvquestion, tvClear, tvQNo;
        Button optA, optB, optC, optD;

        public CustomViewHolder(View itemView) {
            super(itemView);
            tvQNo = (TextView) itemView.findViewById(R.id.tvLiveQuizQuestionNo);
            tvquestion = (TextView) itemView.findViewById(R.id.tvLiveQuizQuestion);
            optA = (Button) itemView.findViewById(R.id.buttonOptionA);
            optB = (Button) itemView.findViewById(R.id.buttonOptionB);
            optC = (Button) itemView.findViewById(R.id.buttonOptionC);
            optD = (Button) itemView.findViewById(R.id.buttonOptionD);
            tvClear = (TextView) itemView.findViewById(R.id.tvClearSelection);
        }
    }
}

更新 - 请检查我是否在适配器中添加了 setQuestionList 函数。请使用此功能设置您的问题列表。因为,我认为在初始化您的适配器时,正在传递的问题列表的大小为零。

关于java - Android RecyclerView 项目选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55776974/

相关文章:

android - 如何按名称(字符串)而不是整数获取资源

Android Recyclerview,okhttp,收到java.lang.ClassCastException错误

android - Kotlin 类型不匹配 : inferred type is View! 但 TextView 是预期的

android - LG手机无法访问SqLite数据库

java - 在 Firebase RecyclerView Cardview 中单击项目时如何打开详细信息 Activity

java - 部署的 org.apache.cxf.binding.AbstractBaseBindingFactory/bus 需要映射名称

java - 让 InputStream 多次读取,不管 markSupported()

java - Spring boot elasticsearch @Field name 不起作用

java - 如何使用 Apache Tika 1.5 解析大文本文件?

Android 应用暂停 onStop