Android: Default preference value not being set from XML
I have a simple Preferences Activity that I populate via XML, defining the values as array resources.
In the MAIN Activity of the application, I get a handle to this via:
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
In the XML for the only preference I have, which is a display setting, I have the following XML that defines the ListPreference:
<ListPreference
android:title="@string/pref_title_sort"
android:summary="@string/pref_summary_sort"
android:key="@string/pref_key_sort"
android:defaultValue="modified"
android:entries="@array/sort_order"
android:entryValues="@array/sort_order_values" />
You can see I am trying to set the default value to 'modified', which is a value found in @array/sort_order_values:
<string-array name="sort_order_values">
<item>modified</item>
<item>created</item>
<item>name</item>
</string-array>
However, when the Preferences Activity is launched, none of the items are selected by default.
I've tried adding the following line to my Activity, but it开发者_StackOverflow中文版 did not change anything (where pref_main is the XML file that defines the preferences):
PreferenceManager.setDefaultValues(this, R.xml.pref_main, false);
Any help appreciated!
Paul
Maybe You have just set a wrong value (without corresponding item in the value array) at the first time you run application. Now Android remembers your first choice. Try to manually uninstall app (Menu >> Settings >> Applications >> Manage Applications >> >> Uninstall ). This should help.
精彩评论