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

学习APP测验功能开发:保存用户答案至对象及回显更新实现

嘿,你已经把题目的检索、存储和切换逻辑跑通了,接下来要搞定用户答案的保存和状态回显对吧?这部分其实核心就是让用户的选择和你的QuestionObject数据保持双向同步,我给你拆解具体的实现步骤和代码示例:

第一步:给QuestionObject添加存储用户答案的字段

首先得让你的数据模型能存用户选的答案,在QuestionObject里加个字段和对应的getter/setter:

public class QuestionObject {
    // 你已有的字段:比如题目内容、选项列表等
    private String questionContent;
    private List<String> options;
    
    // 新增:存储用户选中的答案
    private String userSelectedAnswer;

    // Getter & Setter
    public String getUserSelectedAnswer() {
        return userSelectedAnswer;
    }

    public void setUserSelectedAnswer(String userSelectedAnswer) {
        this.userSelectedAnswer = userSelectedAnswer;
    }

    // 原有构造方法和其他逻辑...
}
第二步:监听单选按钮选择,实时保存答案到对象

当用户选中某个选项时,要立刻把这个选择同步到当前的QuestionObject里。假设你用的是RadioGroup来管理单选按钮,给它加个选择监听:

// 假设你的RadioGroup实例是radioGroup,currentIndex是当前显示的题目索引
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
    // 获取选中的单选按钮文本
    RadioButton selectedBtn = group.findViewById(checkedId);
    String selectedAnswer = selectedBtn.getText().toString();
    
    // 找到当前题目对象,更新它的选中答案
    QuestionObject currentQuestion = questionObject.get(currentIndex);
    currentQuestion.setUserSelectedAnswer(selectedAnswer);
});

这里要注意:如果用户修改了之前选过的答案,这个监听会再次触发,自动覆盖对象里的旧值,完美解决“修改答案同步更新对象”的需求~

第三步:切换题目时,自动回显已选中的答案

不管是点上一题还是下一题,切换到对应题目后,都要从QuestionObject里取出之前保存的答案,然后把对应的单选按钮设为选中状态。

上一题按钮逻辑

previousBtn.setOnClickListener(v -> {
    if (currentIndex > 0) {
        currentIndex--;
        loadCurrentQuestion();
    }
});

下一题按钮逻辑

nextBtn.setOnClickListener(v -> {
    if (currentIndex < questionObject.size() - 1) {
        currentIndex++;
        loadCurrentQuestion();
    }
});

核心的loadCurrentQuestion()方法(统一处理题目显示和答案回显)

private void loadCurrentQuestion() {
    QuestionObject question = questionObject.get(currentIndex);
    
    // 1. 更新题目内容和选项显示(你之前已经实现的逻辑)
    tvQuestion.setText(question.getQuestionContent());
    List<String> options = question.getOptions();
    // 假设你有多个RadioButton:rbOption1, rbOption2...
    rbOption1.setText(options.get(0));
    rbOption2.setText(options.get(1));
    // ...其他选项的设置
    
    // 2. 回显已选中的答案
    String savedAnswer = question.getUserSelectedAnswer();
    if (savedAnswer != null) {
        // 遍历RadioGroup里的按钮,找到匹配的文本并选中
        for (int i = 0; i < radioGroup.getChildCount(); i++) {
            RadioButton rb = (RadioButton) radioGroup.getChildAt(i);
            if (rb.getText().toString().equals(savedAnswer)) {
                rb.setChecked(true);
                break;
            }
        }
    } else {
        // 如果没有保存的答案,清空RadioGroup的选中状态
        radioGroup.clearCheck();
    }
}
关键注意点
  • 确保每次切换题目时,先更新选项的文本,再执行答案回显的逻辑,不然可能会出现“选中了旧选项文本”的问题。
  • 如果你的选项是动态生成的(比如不是固定数量的RadioButton),那在loadCurrentQuestion()里要先清空RadioGroup,再重新添加选项,然后再回显答案。

这样一套逻辑下来,用户的选择会实时存在QuestionObject里,切换题目时自动恢复之前的选择,修改答案也会同步更新对象,完全满足你的需求啦~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:26:27