Skip to content Skip to sidebar Skip to footer

Getting Integer Or Index Values From A List Preference

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 some cases.

Solution 1:

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.

Solution 2:

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.

Solution 3:

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">Numberof logs per cache</string>
<string-arrayname="pref_download_logs_count_titles"><item>10</item><item>20</item><item>50</item><item>Give me all!</item></string-array><integer-arrayname="pref_download_logs_count_values"><item>10</item><item>20</item><item>50</item><item>-1</item></integer-array>

Solution 4:

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.

publicclassListIntegerPreferenceextendsListPreference
{
    publicListIntegerPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
    {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    publicListIntegerPreference(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    publicListIntegerPreference(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    publicListIntegerPreference(Context context)
    {
        super(context);
    }

    @OverrideprotectedvoidonSetInitialValue(boolean restoreValue, Object defaultValue)
    {
        intdefValue= defaultValue != null ? Integer.parseInt((String)defaultValue) : 0;
        intvalue= getValue() == null ? 0 : Integer.parseInt(getValue());
        this.setValue(String.valueOf(restoreValue ? this.getPersistedInt(value) : defValue));
    }

    @OverridepublicvoidsetValue(String value)
    {
        try
        {
            FieldmValueField= ListPreference.class.getDeclaredField("mValue");
            mValueField.setAccessible(true);
            FieldmValueSetField= ListPreference.class.getDeclaredField("mValueSet");
            mValueSetField.setAccessible(true);

            StringmValue= (String)mValueField.get(this);
            booleanmValueSet= (boolean)mValueSetField.get(this);

            booleanchanged= !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...

Post a Comment for "Getting Integer Or Index Values From A List Preference"