Is it possible to define a ListPreference in Xml and retrieve the value from SharedPreferences using getInt? Here is my Xml:
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.
I did this in a little more extreme way.
I used the ListPreference and made my entryValues array contain Strings that I can convert to integer with Integer.parseInt().
Then in my PreferencesActivity, I setup a OnPreferenceChangeListener for this preference, and in the onPreferenceChange() method I set a different preference to the integer version - this second one is the one I actually use in my code. The first is there just for the user option.
This way I don't have to convert a String to int each time I need to look at it, I just do it when the user sets it. Perhaps overkill, but it does work :)
In fact as you can read in the docs:
http://developer.android.com/reference/android/preference/ListPreference.html#getValue()
The method to get a ListPreference
value is:
public String getValue ()
So you get a String. It's not a big deal, but could be prettier to admit integers.