2011-12-30 69 views
0

我想從共享首選項中檢索「entries」而不是「entryValue」。我使用這一點,它得到entryValue:如何獲取共享首選項的值

String notifyInterval = PreferenceManager.getDefaultSharedPreferences(mActivity).getString(ACCUWX.Preferences.PREF_NOTIFY_INTERVAL, null); 

這裏是XML和數組文件:

<ListPreference 
android:key="pref_temp_notifications" 
android:title="@string/notifications" 
android:entries="@array/pref_temp_notifications" 
android:entryValues="@array/pref_temp_notifications_values" 
android:dialogTitle="@string/notifications" 
android:defaultValue="2"/> 

<string-array name="pref_temp_notifications"> 
    <item>@string/my_current_location</item> 
    <item>@string/home_location</item> 
    <item>@string/off</item> 
</string-array> 
<string-array name="pref_temp_notifications_values"> 
    <item>0</item> 
    <item>1</item> 
    <item>2</item> 
</string-array> 

所以我想檢索字符串值,而不是數字。數字是我返回並分配給我的變量notifyInterval。我如何抓取文字?

+0

爲什麼不直接替換'0,1,2'以字符串值 – Wozza 2011-12-30 13:31:17

+0

我不這樣做,因爲有我需要兩個字符串值,在代碼中計算的整數值,其他首選項。 – taraloca 2011-12-30 14:24:05

+0

SharedPreferences的getAll()方法將返回所有鍵值對。從這張地圖你可以得到所有關鍵值。 – Yury 2011-12-30 14:36:23

回答

0

您需要使用返回Map的getAll()方法,從該地圖獲取KeySet,該KeySet返回共享首選項的條目。

Map<String, ?> test = getAll(); 
Set keySet = test.keySet(); 

Iterator<String> keySetIter = keySet .iterator(); 
    while (keySetIter.hasNext()) { 
    String keyEntry= keySetIter.next(); 
} 
+0

我試過這個,它返回的不是存儲的值。 – taraloca 2011-12-30 16:24:50

+0

我認爲這就是你要找的?你知道獲得共享首選項的關鍵嗎?如果是這樣,你可以使用這樣的東西,如果你的值是Integer,你需要使用getInt(),而不是getBoolean(....)SharedPreferences設置= getSharedPreferences(PREFS_NAME,0); boolean silent = settings.getBoolean(「silentMode」,false); – kosa 2011-12-30 16:52:58

+0

這將返回entryValue,我需要條目。我可以在我的活動中像這樣擴展PreferenceActivity,其中「key」是我的首選項:Preference pref = findPreference(key); \t \t String currentPrefValue = null; (ListPreference的實例){ \t ListPreference listPref =(ListPreference)pref; \t currentPrefValue =(listPref.getEntry())。toString(); (DEBUG_TAG,「onSharedPreferenceChanged中的當前pref值爲」+ currentPrefValue); \t} – taraloca 2011-12-30 17:07:34