简体   繁体   中英

Shared-preference always returns default value

I've got my shared preference's setup so it uses 2 arrays. All my ListPreference works except one which uses a different android:entryValues.

I've searched high and low to try and find out how to recall android:entryValues but can't find anything specific to this problem.

When I use the key name it crashes.

Heres the code;

XML Preferences.

<ListPreference
    android:title="Item Size"
    android:summary="Set the item size."
    android:key="itemSize"
    android:defaultValue="Medium"
    android:entries="@array/itemSize"
    android:entryValues="@array/itemSizeNumbers"
/>

XML Values

<string-array name="itemSize">
    <item>Small</item>
    <item>Medium</item>
    <item>Large</item>
</string-array>
<string-array name="itemSizeNumbers">
    <item>8</item>
    <item>12</item>
    <item>18</item>
</string-array>

The _items_screen_percentage always returns 12 no matter what.

this._items_screen_percentage = this.prefs.getInt("itemSizeNumbers", 12);
Log.w("Item Size",Integer.toString(this._items_screen_percentage));

Does anyone know of an easy way to get EntryValues from shared preferences?

The key name is itemSize , so you should query the preferences for itemSize , not itemSizeNumbers . Besides, it's a string array, so your result will be a string, not an integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM