【问题标题】:How to save and retrieve value of radiobutton to SharedPreferences from a multiple choice question如何从多项选择问题中保存和检索单选按钮的值到 SharedPreferences
【发布时间】:2019-07-02 16:49:38
【问题描述】:

我正在开发一个测验应用程序。有 10 个问题,每个问题有 4 个选项(单选按钮)。从技术上讲,用户从第一个问题中选择一个单选按钮,然后按“下一步”按钮继续第二个问题。我想保存用户对每个问题的回答,以便它可以显示在 recyclerview 的另一个活动中,并且用户可以查看他们的答案。我对如何保存值并将其显示在 recyclerview 的另一个活动中感到困惑,那我该怎么办?请帮助我,提前谢谢。

这是我的活动的一些代码:

private ArrayList<Task> tasks;
//some declaration
    TextView task_question, task_header, timer, count;
    RadioGroup choices_group;
    RadioButton choice_A, choice_B, choice_C, choice_D;
    Button next, previous;

    ProgressDialog loading;
    Token auth = PreferencesConfig.getInstance(this).getToken();
    String token = "Bearer " + auth.getToken();

    int score;
    private int currentTaskId = 0;
    String task_answer;

//onCreate method
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banksoal_test);

        final Intent intent = getIntent();
        String judul = intent.getStringExtra("task_title");
        task_header = findViewById(R.id.task_header);
        task_header.setText(judul);

        timer = findViewById(R.id.time);
        task_question = findViewById(R.id.pertanyaan);
        choices_group = findViewById(R.id.rg_question);
        choice_A = findViewById(R.id.option_A);
        choice_B = findViewById(R.id.option_B);
        choice_C = findViewById(R.id.option_C);
        choice_D = findViewById(R.id.option_D);
        count = findViewById(R.id.count);
        next = findViewById(R.id.bNext);
        previous = findViewById(R.id.bPrevious);

    }


//.....


//this is function to retrieve the question
 public void showQuestion(){
        Task task = tasks.get(currentTaskId);
        task_question.setText(task.getSoal());
        choice_A.setText(task.getOption_A());
        choice_B.setText(task.getOption_B());
        choice_C.setText(task.getOption_C());
        choice_D.setText(task.getOption_D());
        task_answer = task.getJawaban();
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedId = choices_group.getCheckedRadioButtonId();
                RadioButton selectedRB = findViewById(selectedId);
                if (selectedRB.getText().toString().equals(task_answer)){
                   score+=10;
                }

                if (currentTaskId < tasks.size() - 1){
                    currentTaskId++;
                    showQuestion();
                    selectedRB.setChecked(false);
                    choices_group.clearCheck();

                }else {
                    Intent intent = new Intent(TaskActivity.this, ResultActivity.class);
                    intent.putExtra("score", score);
                    startActivity(intent);
                }
            }
        });

【问题讨论】:

    标签: android arraylist radio-button


    【解决方案1】:

    使用 SharedPreferences 你可以做到这一点:

    int answer_number = 4;
    String answer = "the answer of the user";
    
    //Your id is the identifier and location of your SharedPreferences because you can have
    //multiple instances of SharedPrefernces.
    String id = "quiz-application";
    
    SharedPreferences sharedPref = getActivity().getSharedPreferences(id, Context.MODE_PRIVATE);
    
    //The editor
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(String.valueOf(answer_number), answer);
    editor.apply();
    

    通过这样做,您可以将答案保存在“quiz_application”.sharedpreferences 文件中,然后通过以下操作访问它们:

    SharedPreferences sharedPref = getActivity().getSharedPreferences(id, Context.MODE_PRIVATE);
    String answer = sharedRef.getString("1", "no answer found");
    //The 1 being the answer number while the second parameter being a fail-safe, 
    //if there isn't anything there it would return "no answer found".
    

    请记住,您仍然需要 id 和答案号码才能获取信息。在这种情况下,答案很简单,它只能取这些值:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}。你需要照顾的是id。

    【讨论】:

    • 好的,谢谢,我试试。如有错误我稍后会通知您
    • 太棒了。如果有错误请正确指定。
    • 我已经按照你的指示做了,我仍然对如何以及在哪里调用 sharedpref 感​​到困惑?在另一个活动中的适配器上?如果你记得我用的是回收站视图。
    • 第一个选项使用它来保存单选按钮选择并将其放在您转到下一个活动的按钮。第二个选项将它放在要显示选择的活动中。
    猜你喜欢
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2017-03-28
    • 2011-07-01
    • 2021-02-16
    • 2011-06-07
    • 1970-01-01
    • 2012-12-30
    相关资源
    最近更新 更多