1

我正在创建一个测验应用程序,我提出了一些问题并以单选按钮的形式提供选项,现在我想将答案的值(正确答案加上,错误答案减去)存储在其中SharedPreferences并在其他活动中显示该结果. 我在这里搜索并找到了这个答案

我使用了它,但仍然无法获得我想要的结果我的代码看起来像:

主要活动可以节省一些价值SharedPreferences

public class MainActivity extends Activity {
private SharedPreferences saveScore;
private SharedPreferences.Editor editor;
RadioGroup group;
RadioButton radioButton;    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    group = (RadioGroup) findViewById(R.id.radioGroup1);
    radioButton = (RadioButton) group.findViewById(group.getCheckedRadioButtonId());
    saveScore = getPreferences(MODE_PRIVATE);       
}
public void gotoNextAndSaveScore(View view) {
    if(group.getCheckedRadioButtonId() != R.id.radio3){
        editor = saveScore.edit();
        editor.putInt("score", -1);
        editor.commit();
    }else{
        editor = saveScore.edit();
        editor.putInt("score", 1);
        editor.commit();
    }       
    Intent intent = new Intent (MainActivity.this, NextActivity.class);
    startActivity(intent);
}}

这是下一个尝试从以下位置获取值的活动SharedPreferences

public class NextActivity extends Activity{

private SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);

    preferences = this.getSharedPreferences("score", MODE_WORLD_WRITEABLE);

    int value = preferences.getInt("score", 0);
    String score = "Your Score is : " + value;
    UIHelper.displayScore(this, R.id.tvScore, score );

}}

有人知道怎么做吗?

4

4 回答 4

3

你应该换行

 saveScore = getPreferences(MODE_PRIVATE);      

saveScore =  getSharedPreferences("score",Context.MODE_PRIVATE);
于 2013-02-26T06:41:36.487 回答
1

试试这个,

public class DataStorage {

private static String KEY;
public static SharedPreferences savedSession;

public void saveID(Context context, String msessionid) {
    // TODO Auto-generated method stub

    Editor editor = context
            .getSharedPreferences(KEY, Activity.MODE_PRIVATE).edit();
    editor.putString("SESSION_UID", msessionid);
    editor.commit();
}

public String getID(Context context) {

    savedSession = context.getSharedPreferences(KEY, Activity.MODE_PRIVATE);
    return savedSession.getString("SESSION_UID", "");
}
}

编辑:

 DataStorage mdata = new DataStorage();

public void gotoNextAndSaveScore(View view) {
if(group.getCheckedRadioButtonId() != R.id.radio3){

   mdata.saveId(Mainactivity.this,1);
}else{
   mdata.saveId(Mainactivity.this,-1);
}   

然后从中获取价值NextActivity

DataStorage mdata = new DataStorage();
mdata.getId(NextActivity.this);
于 2013-02-26T06:38:55.390 回答
1

存储导航到nextActivity时的值onPause(),如下所示:

    @Override
    protected void onPause() 
    {
      super.onPause(); 
      // Store values between instances here
      SharedPreferences preferences = getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = preferences.edit();
      editor.putString("YourStringKeyValue", "StringValue"); // value to store
      // Commit to storage
      editor.commit();
    }

并使用该键获取数据,next Activity如下onCreate所示:

       SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
       String name= preferences.getString("YourStringKeyValue","");
于 2013-02-26T06:40:31.520 回答
0

您使用了错误的首选项文件,Activity 上的 getPreference 函数返回该 Activity 私有的文件。您需要使用命名文件版本- getSharedPreferences(name, mode)

于 2013-02-26T06:40:06.777 回答