Android fragments 无法从preference.xml检索android首选项
我一直在看android的偏好,但我想我一定是搞错了什么。 我的基本想法是在res/xml中有一个preference.xml文件,它存储一组数据,包括ListPreference等。 我已经在XML中设置了默认值,但是当我试图在单独的片段中检索这些值时,它似乎找不到首选项 我的片段中函数的代码:Android fragments 无法从preference.xml检索android首选项,android-fragments,android-preferences,Android Fragments,Android Preferences,我一直在看android的偏好,但我想我一定是搞错了什么。 我的基本想法是在res/xml中有一个preference.xml文件,它存储一组数据,包括ListPreference等。 我已经在XML中设置了默认值,但是当我试图在单独的片段中检索这些值时,它似乎找不到首选项 我的片段中函数的代码: public static void attemptToAddToCalendar(final Context context, final SessionEntity session){
public static void attemptToAddToCalendar(final Context context, final SessionEntity session){
SharedPreferences sharedPreferences = context.getSharedPreferences("preferences", Context.MODE_PRIVATE);
Toast.makeText(context, "preferences are" + sharedPreferences.getString("pref_addToCalendarAutomatically-list", "Unknown"), Toast.LENGTH_LONG).show();
preferences.xml的代码
阵列
现在,这将只输出未知的文本首选项,这显然意味着它找不到值。但是我不明白为什么。谢谢你的评论!事实上,这只是布局。事后看来,这是很明显的,但我有点盲目 对于遇到相同问题的任何人,我修改了我的代码,如下所示: 片段函数:
public static void attemptToAddToCalendar(final Context context, final SessionEntity session){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Toast.makeText(context, "preferences are " + sharedPreferences.getString("prefAddToCalendarAutomaticallylist", "Unknown"), Toast.LENGTH_LONG).show();
相应地,XML:
<ListPreference
android:key="prefAddToCalendarAutomaticallyList"
android:entries="@array/prefARaddToCalendarAutomatically"
android:entryValues="@array/prefARaddToCalendarAutomaticallyValues"
android:defaultValue="2"
android:summary="Add favorites to calendar"
android:title="Calendar Options" >
</ListPreference>
这让我快发疯了,再次感谢你 1-in/res/xml只是首选项屏幕的布局。2-您的首选项文件位于/data/data/Your.app.name/shared_prefs/Your.app.name_preferences.xml中3由于-
<ListPreference
android:key="prefAddToCalendarAutomaticallyList"
android:entries="@array/prefARaddToCalendarAutomatically"
android:entryValues="@array/prefARaddToCalendarAutomaticallyValues"
android:defaultValue="2"
android:summary="Add favorites to calendar"
android:title="Calendar Options" >
</ListPreference>