我正在研究测验应用程序。有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);
}
}
});
答案 0 :(得分:0)
使用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。