DefaultPreferences VS. SharedPreferences

时间:2014-10-02 17:36:10

标签: android sharedpreferences preferenceactivity

几个月我已经将defaultPreferences用于多种需求,但是对于当前的需求,我需要多个Preferences,因此,必须将第二个添加到项目中。所以我添加了Sharedpreferences, 我所做的就像我对defaultPreferences所做的一样。但我得到的结果是" NULLY"对于SharedPrefernces!但是我得到了我想要的东西! defaultPreferences

我的Pref.xml是:

<EditTextPreference
        android:key="t2"
        android:summary="some text"
        android:title="some T"
        android:layout_gravity="right"
        android:defaultValue="2"
    />
    <ListPreference
        android:key="mt2"
        android:summary="some text2"
        android:title="some T2"
        android:layout_gravity="right"
        android:defaultValue="2"
        android:entries="@array/mt2_setting"
        android:entryValues="@array/mt2_settingsvalues"/>

我已经知道了这个SharedPreferences。我从OnCreate运行了以下部分,从内部方法但没有运气,只返回NULLY,

    SharedPreferences Prefs=context.getSharedPreferences("pref", Context.MODE_PRIVATE);
    String t2TEMP=Prefs.getString("t2", "NULLY");
    String mt2TEMP=Prefs.getString("mt2", "NULLY");
     Log.v("PREFERENCES VALUE:", "mt2 is: "+mt2TEMP+"*"+t2TEMP);

我使用的上下文是context=this.getBaseContext();,虽然我再次尝试this但没有运气。

如果有任何帮助,我会在调用首选项PreferenceManager.setDefaultValues(this, R.xml.prefe, false);之前应用默认值 我真的想知道阻止我的DefaultShared偏好之间有什么不同。

1 个答案:

答案 0 :(得分:0)

我很确定你要找的是

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

因为首选项会保存在那里。您指向另一个SharedPreference文件