【发布时间】:2016-08-08 11:29:21
【问题描述】:
我添加了三个共享首选项,如下代码所示。而且我能够仅检索共享偏好值。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("Loggedin",true);
editor.putString("userId",userid);
editor.putString("pwd",password);
editor.apply();
editor.commit();
我使用以下代码从另一个活动中检索。我只能检索布尔值。其他值不存在。获取字符串值的默认值。请帮帮我。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Boolean loggedin=preferences.getBoolean("Loggedin", false);
String userId=preferences.getString("userId", "0");
String pwd=preferences.getString("pwd", "0");
【问题讨论】:
-
您是否获得了其他活动的价值?
-
布尔登录=preferences.getBoolean("Loggedin", false);如何产生偏好
-
@Singh 是的。我正在检索另一个活动的值
-
发布完整代码,您在尝试从中检索值时如何实例化您的偏好。
-
你为什么同时使用
apply()和commit()?!