Android RecyclerView项目选择问题

时间:2019-04-20 19:27:24

标签: 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);

        }
    } 
}

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

请仅在未选择的选项上帮助我。预先感谢。

1 个答案:

答案 0 :(得分:2)

视图将在您的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功能。请使用此功能设置您的问题清单。因为,我认为在初始化适配器时,正在传递的问题列表的大小为零。