【发布时间】: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