0

嗨,我正在尝试从列表视图中保存用户首选项,我希望它存储选择的内容,然后当应用程序再次加载时检查首选项选择的方法,如果它 = 团队然后做某事,但目前它没有在偏好中加载任何关于我做错了什么的想法。当我注销选择的方法时,它显示为空

这是我的代码

preferences.edit().putString("ChosenMethod", "Team").commit();
    preferences.edit().putString("ChosenTeam", ChosenTeam).commit();
    preferences.edit().putString("ChosenTeamId", ChosenTeam).commit();
    preferences.edit().putString("ChosenLeagueId", ChosenTeam).commit();
    preferences.edit().putString("ChosenDivisionID", ChosenTeam).commit();

然后在 introActivity 我把这个

protected void checkPreferences(){


    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
     Log.v("myapp", "prefs = " + preferences);
     String ChosenMethod = preferences.getString("ChosenTeam", chosenMethod);
     Log.v("myapp", "ChosenMethod = " + ChosenMethod);

     if (ChosenMethod != null){

         Intent intent = new Intent(TheEvoStikLeagueActivity.this,Activity.class);

     }




}


            @Override

            public void onCreate(Bundle icicle) {

                    super.onCreate(icicle);

                    setContentView(R.layout.main);
                    checkPreferences();
4

1 回答 1

0
//Retrive value from SharedPreference
public static String getStringFromSP(String key) {
// TODO Auto-generated method stub
    SharedPreferences preferences = getApplicationContext().getSharedPreferences(" SHARED_PREFERENCES_NAME ", android.content.Context.MODE_PRIVATE);
    return preferences.getString(key, null);
}//getPWDFromSP()

您可以使用上面的代码从 SharedPreference 中获取值。参考这个链接

于 2012-05-08T16:08:52.483 回答