如何从多项选择问题中保存和检索RadioButton的价值为共享流程



我正在处理测验应用程序。有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);
                }
            }
        });

使用共享流程可以做到这一点:

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。

最新更新