2013-08-21 40 views
0

我面臨着共享偏好問題共享偏好..如何使用測驗

在我的應用程序使用的IM保存測驗答案共享偏好..

我都試過了。它的工作很好,當我來到perivous問題..但我的問題是..

當第一個問題出現時,用戶選擇一個答案和第二個問題,這裏用戶也會選擇答案和第三個問題,如明智的過程移動..當用戶回來問題(第二個問題)時,這裏用戶回答正確的檢查按鈕,當用戶下次到這個時候答案沒有正確檢查...我做了什麼錯誤?

請指出我..並給出一個解決方案如何得到那個?非常感謝提前..

對不起我的英文不好..

這裏是我的代碼..

public class MainActivity extends Activity { 
    RadioGroup btn_practicerg; 
    RadioButton btn_practice1; 
    RadioButton btn_practice2; 
    RadioButton btn_practice3; 
    RadioButton btn_practice4; 
    RadioGroup radioButton; 
    TextView quetxt; 
    Button next; 
    Button previous; 
    int i = 0, k = 0; 
    int checkedIndex; 
    int num = 1; 
    private ArrayList<String> answ1 = new ArrayList<String>(); 
    private ArrayList<String> ques1 = new ArrayList<String>(); 
    final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btn_practicerg = (RadioGroup) findViewById(R.id.rdgroup); 
     btn_practice1 = (RadioButton) findViewById(R.id.RB1); 
     btn_practice2 = (RadioButton) findViewById(R.id.RB2); 
     btn_practice3 = (RadioButton) findViewById(R.id.RB3); 
     btn_practice4 = (RadioButton) findViewById(R.id.RB4); 
     quetxt = (TextView) findViewById(R.id.que_txt); 
     next = (Button) findViewById(R.id.nxt_btn); 
     previous = (Button) findViewById(R.id.accbtn); 

     runOnUiThread(new Runnable() { 
      public void run() { 
       LoadQuestions(); 
      } 
     }); 

     next.setOnClickListener(new Button.OnClickListener() { 
      public void onClick(View v) { 

       int selectedId = btn_practicerg.getCheckedRadioButtonId(); 

       // find the radiobutton by returned id 
       RadioButton radioSexButton = (RadioButton) findViewById(selectedId); 
       checkedIndex = btn_practicerg.indexOfChild(radioSexButton); 
       Toast.makeText(MainActivity.this, radioSexButton.getText(), 
         Toast.LENGTH_SHORT).show(); 

       if (i == ques1.size() - 1) { 
        showAlert(); 
       } else { 

        ++i; 
        ++num; 
        TextView quetxt = (TextView) findViewById(R.id.que_txt); 
        quetxt.setText("Q" + num + ")" + ques1.get(i)); 

        ++k; 
        btn_practice1.setText(answ1.get((k * 4) + 0)); 
        btn_practice2.setText(answ1.get((k * 4) + 1)); 
        btn_practice3.setText(answ1.get((k * 4) + 2)); 
        btn_practice4.setText(answ1.get((k * 4) + 3)); 
        btn_practicerg.clearCheck(); 

        SavePreferences(String.valueOf(num), checkedIndex); 

       } 
      } 

      private void showAlert() { 
       // TODO Auto-generated method stub 

      } 

     }); 

     Button previousbtn1 = (Button) findViewById(R.id.accbtn); 
     previousbtn1.setOnClickListener(new Button.OnClickListener() { 
      public void onClick(View v) { 
       --i; 
       --num; 
       TextView quetxt = (TextView) findViewById(R.id.que_txt); 
       quetxt.setText("Q" + num + ")" + ques1.get(i)); 

       --k; 
       btn_practice1.setText(answ1.get((k * 4) + 0)); 
       btn_practice2.setText(answ1.get((k * 4) + 1)); 
       btn_practice3.setText(answ1.get((k * 4) + 2)); 
       btn_practice4.setText(answ1.get((k * 4) + 3)); 

       LoadPreferences(); 
      } 
     }); 

    } 

    private void LoadQuestions() { 
     ques1.add("whats the name?"); 
     ques1.add("whats place?"); 
     ques1.add("whats the favourite?"); 
     ques1.add("whats the game?"); 
     ques1.add("whats the time?"); 

     answ1.add("A"); 
     answ1.add("B"); 
     answ1.add("C"); 
     answ1.add("D"); 
     answ1.add("MDU"); 
     answ1.add("MS"); 
     answ1.add("CHE"); 
     answ1.add("POND"); 
     answ1.add("1"); 
     answ1.add("2"); 
     answ1.add("3"); 
     answ1.add("4"); 
     answ1.add("VB"); 
     answ1.add("TENN"); 
     answ1.add("HOC"); 
     answ1.add("CRI"); 
     answ1.add("11"); 
     answ1.add("12"); 
     answ1.add("13"); 
     answ1.add("14"); 

     quetxt = (TextView) findViewById(R.id.que_txt); 
     quetxt.setText("Q" + num + ")" + ques1.get(i)); 

     btn_practice1.setText(answ1.get(0)); 
     btn_practice2.setText(answ1.get(1)); 
     btn_practice3.setText(answ1.get(2)); 
     btn_practice4.setText(answ1.get(3)); 
    } 

    private void SavePreferences(String key, int value) { 
     int quest = (Integer.parseInt(key)) - 1; 
     SharedPreferences preferences = getSharedPreferences("MY_SHARED_PREF", 
       0); 
     SharedPreferences.Editor editor = preferences.edit(); 

     editor.putInt(String.valueOf(quest), value); 
     editor.commit(); 
    } 

    private void LoadPreferences() { 

     SharedPreferences getProgramPrefs = this.getSharedPreferences(
       "MY_SHARED_PREF", MODE_WORLD_READABLE); 

     int savedRadioIndex = getProgramPrefs.getInt(String.valueOf(num), -1); 

     System.out.println("SavedRadioIndex" + savedRadioIndex); 
     RadioButton savedCheckedRadioButton = (RadioButton) btn_practicerg 
       .getChildAt(savedRadioIndex); 
     savedCheckedRadioButton.setChecked(true); 
    } 

    OnCheckedChangeListener radioGroupOnCheckedChangeListener = new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      RadioButton radioButton = (RadioButton) group 
        .findViewById(checkedId); 
      checkedIndex = group.indexOfChild(radioButton); 
      System.out.println("checkedIndex" + checkedIndex); 
     } 
    }; 

} 

回答

0

這不是很好的偏好保存這些數據。如果您想要在應用程序完成並重新啓動時獲取數據,最好將這些數據保存在數據庫中。但是,在這樣的情況下,你可以像這樣的活動存儲在某個對象的數據:

public class Question { 
private int id; 
private String question; 
private List<Answer> possibleAnswers; 
private int correctAnswerId; 
private String userAnswer; 
} 
0

你嘗試過調試觀看SavePreferences法「追求」變量的值?從String到int以及從int到String再次有兩次不必要的轉換,也許這可能是問題,具體取決於「key」變量的值。

您應該嘗試直接使用「key」變量作爲編輯鍵,而無需強制轉換。

該函數的其餘部分似乎是正確的。

編輯 我在說的是,也許這個演員陣容有問題,但我不確定。

private void SavePreferences(String key, int value) { 
    SharedPreferences preferences = getSharedPreferences("MY_SHARED_PREF",0); 
    SharedPreferences.Editor editor = preferences.edit(); 

    editor.putInt(key, value); 
    editor.commit(); 
} 
+0

我不明白..你可以編輯代碼並解釋.. –

+0

它不工作..怎麼檢查sharedpref條件... –

相關問題