2013-04-12 65 views
0

我在做基於測驗的應用程序。如何檢查存儲在sharedpreferences中的單選按鈕值?

所以我有近50個問題和4個選項(電臺組)爲每個問題。 我的問題是共享首選項。

我已將用戶答案存儲在共享的推理中,並且當我進入下一個問題並且涉及到在共享推理中獲得用戶答案時的前面的問題,但是如果我再次發表含蓄的問題即時通訊沒有得到該問題的答案。例如)如果我回答9問題,並來到8問題即時獲得這個答案,再次即時來到7或6或5問題,我沒有得到這些答案。

什麼即時做錯了?這個怎麼做?

在此先感謝....

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(); 
     } 
      }); 

當點擊前面的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",""); 
     } 
     }); 

我嘗試這樣的..

+0

我會建議你使用sqllite數據庫。它是更好的解決方案。看到這個(http://www.vogella.com/articles/AndroidSQLite/article.html)sqllite在android –

+0

我使用JSON從服務器數據庫檢索數據... –

回答

0

,因爲你是保存所有答案S IN變量相同的共享偏好是option,你必須把它與問題像這樣的電話號碼存入:

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

optionNumber變量是這樣的:

option1問題1

option2問題2

...

option50問題50

+0

我不明白你的答案。我'm new to android ..請引導我.. –

+0

您正在保存您的答案在'選項'的保存變量中,所以每次您寫入OVER之前的值'option' –

相關問題