I\'m creating lists in a shared preference and when the onPreferenceChanged() method is called I want to extract the index of the item in the list or an integer value in som
Here's a ListIntegerPreference
class I use (written for com.android.support:preference-v7:24.0.0
). It overwrites a few methods and converts between Integer
and String
where possible, so that the base ListPreference
does not recognise, that you are working with Integers
instead of Strings
.
public class ListIntegerPreference extends ListPreference
{
public ListIntegerPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
}
public ListIntegerPreference(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
public ListIntegerPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ListIntegerPreference(Context context)
{
super(context);
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
{
int defValue = defaultValue != null ? Integer.parseInt((String)defaultValue) : 0;
int value = getValue() == null ? 0 : Integer.parseInt(getValue());
this.setValue(String.valueOf(restoreValue ? this.getPersistedInt(value) : defValue));
}
@Override
public void setValue(String value)
{
try
{
Field mValueField = ListPreference.class.getDeclaredField("mValue");
mValueField.setAccessible(true);
Field mValueSetField = ListPreference.class.getDeclaredField("mValueSet");
mValueSetField.setAccessible(true);
String mValue = (String)mValueField.get(this);
boolean mValueSet = (boolean)mValueSetField.get(this);
boolean changed = !TextUtils.equals(mValue, value);
if(changed || !mValueSet)
{
mValueField.set(this, value);
mValueSetField.set(this, mValueSet);
this.persistInt(Integer.parseInt(value));
if(changed) {
this.notifyChanged();
}
}
}
catch (NoSuchFieldException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
}
I'm using this with creating ListPreferences
values via code, just try it. It may work right away, or maybe you need to override additional functions. If so, this is a good start and shows you how you can do it...
Put the preferences in as String
and use Integer.parseInt()
. I think there is actually a bug report on the limitation you are referring to but I can't find the link. From experience I can tell you to just use Strings
and save your self a lot of frustration.
Note to other SO users, if you can prove me wrong, I welcome it.
Andrew was correct, the thread is here:
it's still being commented on, and yet no change (as of 2.3.3 anyway).
Integer.parseInt() of .valueOf() will have to work. If valueOf() works without error, use it, as it doesn't allocate as much as parseInt() does, helpful when you NEED to avoid GC like I do.
Based on the Android's ListPreference, I created IntListPreference. The usage is straighforward - simply put this snippet in your preferences xml:
<org.bogus.android.IntListPreference
android:key="limitCacheLogs"
android:defaultValue="20"
android:entries="@array/pref_download_logs_count_titles"
android:entryValues="@array/pref_download_logs_count_values"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_download_logs_count"
/>
and that in strings.xml
<string name="pref_download_logs_count">Number of logs per cache</string>
<string-array name="pref_download_logs_count_titles">
<item>10</item>
<item>20</item>
<item>50</item>
<item>Give me all!</item>
</string-array>
<integer-array name="pref_download_logs_count_values">
<item>10</item>
<item>20</item>
<item>50</item>
<item>-1</item>
</integer-array>