Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an Android ListPreference defined in Xml whose values are integers?

Is it possible to define a ListPreference in Xml and retrieve the value from SharedPreferences using getInt? Here is my Xml:

<ListPreference android:key="@string/prefGestureAccuracyKey"
    android:title="@string/prefGestureAccuracyTitle" android:summary="@string/prefGestureAccuracyDesc"
    android:entries="@array/prefNumberAccuracyLabels" android:entryValues="@array/prefNumberAccuracyValues"
    android:dialogTitle="@string/prefGestureAccuracyDialog"
    android:persistent="true" android:defaultValue="2"
    android:shouldDisableView="false" />

And I want to get the value with something like: int val = sharedPrefs.getInt(key, defaultValue).

At the moment I have to use getString and parse the result.

like image 597
Rob Kent Avatar asked Apr 24 '10 17:04

Rob Kent


1 Answers

My understanding is that ListPreference can only work with string arrays. You'll have to use getString() and convert to integer yourself. See http://code.google.com/p/android/issues/detail?id=2096 for the bug report on this. It doesn't look like Google plans to extend ListPreference to handle anything but strings.

Also: You'll need to store the preference as a string too. Otherwise, your preferences activity will crash when it starts up and tries to read a string value.

like image 190
Edward Falk Avatar answered Oct 23 '22 07:10

Edward Falk