简体   繁体   English

如何检查存储在sharedpreferences中的单选按钮值?

[英]how to check the radio button value that stored in sharedpreferences?

I'm doing quiz based app. 我正在做基于测验的应用程序。

So i have nearly 50 questions and 4 options(radio group) for each question. 所以我有将近50个问题,每个问题有4个选项(无线电组)。 My problem is in shared preferences. 我的问题是共享首选项。

I have stored the users answer in shared perferences and when i goes to next question and comes to pervious question im getting the user answer in shared perferences, but if i goes again to perivious question im not getting that question answer.. For eg) If i answer 9 question and comes to 8 question im getting this answer and again im comes to 7 or 6 or 5 question here im not getting these answers. 我已经将用户答案存储在共享的问题中,当我转到下一个问题并进入上一个问题时,我将用户的答案存储在共享的问题中,但是如果我再次转到上一个问题,则我没有得到该问题的答案。例如:我回答了9个问题,而第8个问题我得到了这个答案,我又回答了7个或6个或5个问题,我没有得到这些答案。

What im doing wrong? 我在做什么错? How to do this? 这个怎么做?

Thanks in advance.... 提前致谢....

  btn_practicerg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId){ RadioButton radioButton = (RadioButton)group. findViewById(checkedId); String temp = radioButton.getText().toString(); SharedPreferences settings =PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = settings.edit(); editor.putString("option", temp); // here temp is the value you want to save editor.commit(); } }); 

When click previous btn 当单击上一个btn时

  ImageView previousbtn1 = (ImageView) findViewById(R.id.prv_btn); previousbtn1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); String optionprev = settings.getString("option",""); } }); 

I try like this.. 我这样尝试

because you are saving all the answers in the same shared preferences variable which is option , you have to save it with the number of the question Like this: 因为您将所有答案都保存在相同的共享首选项变量(即option ,所以必须将其保存为问题编号,如下所示:

int numberOfQuestion = //your question's number;
String optionNumber = "option"+numberOfQuestion;

the optionNumber variable has to be like this: optionNumber变量必须像这样:

option1 for question 1 问题1的option1 1

option2 for question 2 问题2的option2

... ...

option50 for question 50 问题50的option50

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM