You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android问答游戏APP问题:无法关联正确答案与选项及乱序实现

解决方案

1. 重构数据结构,锁定题目与答案的关联

先创建一个实体类QuizItem,把单个题目的所有信息封装在一起,从根源避免数组索引错位的问题:

public class QuizItem {
    private String question;
    private List<String> choices;
    private String correctAnswer;

    public QuizItem(String question, List<String> choices, String correctAnswer) {
        this.question = question;
        this.choices = choices;
        this.correctAnswer = correctAnswer;
    }

    // Getter方法
    public String getQuestion() { return question; }
    public List<String> getChoices() { return choices; }
    public String getCorrectAnswer() { return correctAnswer; }
}

接着在Activity里把资源数组转换成List<QuizItem>,同时处理选项乱序:

// 从资源文件读取原始数据
String[] questionArray = getResources().getStringArray(R.array.questions);
String[] choicesArray = getResources().getStringArray(R.array.choices);
String[] correctAnswersArray = getResources().getStringArray(R.array.correctanswers);

List<QuizItem> quizList = new ArrayList<>();

for (int i = 0; i < questionArray.length; i++) {
    String question = questionArray[i];
    String correctAnswer = correctAnswersArray[i];
    List<String> currentChoices = new ArrayList<>();

    // 方案1:如果choicesArray是全局选项池,随机选23个错误选项+1个正确选项
    List<String> allOptions = new ArrayList<>(Arrays.asList(choicesArray));
    allOptions.remove(correctAnswer);
    Collections.shuffle(allOptions);
    currentChoices.add(correctAnswer);
    currentChoices.addAll(allOptions.subList(0, 23));

    // 方案2:如果choicesArray是按题目分组的(每个题目连续24个选项)
    // for (int j = 0; j < 24; j++) {
    //     currentChoices.add(choicesArray[i*24 + j]);
    // }

    // 打乱当前题目的24个选项顺序
    Collections.shuffle(currentChoices);
    quizList.add(new QuizItem(question, currentChoices, correctAnswer));
}

// 若需要随机出题,打乱整个题目列表
Collections.shuffle(quizList);

2. 调整Adapter逻辑,适配新数据结构

现在Adapter要负责显示当前题目的24个选项,同时处理答题后的切换逻辑:

public class QuizAdapter extends RecyclerView.Adapter<QuizAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private Button questionBtn;
    private Button scoreBtn;
    private List<QuizItem> quizList;
    private List<String> currentChoices;
    private String currentCorrectAnswer;
    private int currentQuestionIndex = 0;
    private int score = 0;

    public QuizAdapter(Context context, Button questionBtn, Button scoreBtn, List<QuizItem> quizList) {
        this.mInflater = LayoutInflater.from(context);
        this.questionBtn = questionBtn;
        this.scoreBtn = scoreBtn;
        this.quizList = quizList;
        // 加载第一道题
        loadCurrentQuestion();
    }

    private void loadCurrentQuestion() {
        if (currentQuestionIndex >= quizList.size()) {
            questionBtn.setText("答题结束");
            return;
        }
        QuizItem currentItem = quizList.get(currentQuestionIndex);
        questionBtn.setText(currentItem.getQuestion());
        currentChoices = currentItem.getChoices();
        currentCorrectAnswer = currentItem.getCorrectAnswer();
        scoreBtn.setText("Score: " + score);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.quiz_option_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String choice = currentChoices.get(position);
        holder.optionTv.setText(choice);

        holder.optionTv.setOnClickListener(v -> {
            boolean isCorrect = choice.equals(currentCorrectAnswer);
            // 更新分数与选项样式
            if (isCorrect) {
                score++;
                holder.optionTv.setBackgroundResource(R.drawable.roundgreen);
            } else {
                score--;
                holder.optionTv.setBackgroundResource(R.color.red);
            }
            scoreBtn.setText("Score: " + score);

            // 延迟切换题目
            new Handler(Looper.getMainLooper()).postDelayed(() -> {
                holder.optionTv.setBackgroundColor(Color.TRANSPARENT);
                currentQuestionIndex++;
                loadCurrentQuestion();
            }, 500);
        });
    }

    @Override
    public int getItemCount() {
        return currentChoices != null ? currentChoices.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView optionTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            optionTv = itemView.findViewById(R.id.info_text);
        }
    }
}

3. 原代码的核心问题说明

  • 原代码每次调用updateQuestion就打乱整个题目数组,导致题目与正确答案的索引对应关系直接失效,因为mCorrectAnswers的索引没有同步变化。
  • 原Adapter的getItemCount返回题目数量,但RecyclerView实际应该显示当前题目的24个选项,数据源与显示逻辑完全不匹配。
  • 用实体类封装单题信息后,能彻底保证题目、选项、正确答案的关联不会被打乱,选项乱序也只针对当前题目的选项列表执行,逻辑更清晰。

内容的提问来源于stack exchange,提问作者TurboAli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 20:42:32