Question

Last couple of days i have been trying to make listpreference work on my application to no avail. I have read countless threads, but still nothing.

Mind you I am still in the learning process, obviously, so my knowledge is rather limited, to say the least.

I am trying to make a program that allows the user to change easily some of the system parameters on a ROM.

public class MyPersArray extends PreferenceActivity implements OnSharedPreferenceChangeListener {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pers);
            getPrefs();

[....]

public String ListPreference; 

    private void getPrefs() {  
        // Get the xml/preferences.xml preferences  
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
        // register preference change listener
        prefs.registerOnSharedPreferenceChangeListener(this);
        ListPreference = prefs.getString("gpsPref", "0");
    }  


    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {      
        if(Integer.valueOf(ListPreference) == 0) {
            ListPreference = prefs.getString("gpsPref", "0");
            Commands.doGps0();
            Toast.makeText(getApplicationContext(), "0",Toast.LENGTH_SHORT).show();
        } else if(Integer.valueOf(ListPreference) == 1) {
            ListPreference = prefs.getString("gpsPref", "1");
            Commands.doGps1();
            Toast.makeText(getApplicationContext(), "1",Toast.LENGTH_SHORT).show();
        } else if(Integer.valueOf(ListPreference) == 2) {
            ListPreference = prefs.getString("gpsPref", "2");
            Commands.doGps2();
            Toast.makeText(getApplicationContext(), "2",Toast.LENGTH_SHORT).show();
        } else if(Integer.valueOf(ListPreference) == 3) {
            ListPreference = prefs.getString("gpsPref", "3");
            Commands.doGps3();
            Toast.makeText(getApplicationContext(), "3",Toast.LENGTH_SHORT).show();
        } else if(Integer.valueOf(ListPreference) == 4) {
            ListPreference = prefs.getString("gpsPref", "4");
            Commands.doGps4();
            Toast.makeText(getApplicationContext(), "4",Toast.LENGTH_SHORT).show();
        } else if(Integer.valueOf(ListPreference) == 5) {
            ListPreference = prefs.getString("gpsPref", "5");
            Toast.makeText(getApplicationContext(), "5",Toast.LENGTH_SHORT).show();
            Commands.doGps5();
        }
    }

This is the code i use for the Preference.

My xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <ListPreference
        android:defaultValue="0"
        android:dialogTitle="Pick your Region"
        android:enabled="true"
        android:entries="@array/gps_entries"
        android:entryValues="@array/gps_values"
        android:key="gpsPref"
        android:negativeButtonText="Cancel"
        android:summary="Enhance GPS signal and speed"
        android:title="GPS Preference" >
    </ListPreference>
</PreferenceScreen>

And my array looks like this:

<string-array name="gps_values">
    <item>0</item>
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
</string-array>
<string-array name="gps_entries">
    <item>Europe</item>
    <item>Africa</item>
    <item>Asia</item>
    <item>Australia</item>
    <item>North America</item>
    <item>South America</item>
</string-array>

The preference code and the getPrefs code is included in the same java file. This is the only way i have found this to "work".

The problem is that with this code, the ListPreference doesn't get updated correctly, and the command isn't executed on click of the ListPreference. It only gets updated if i Hit on the GPS Configuration item again.

Here is a little screenshot so you can understand better. http://i42.tinypic.com/t7bebq.png (can't post images yet)

Thanks a lot for your time.

Was it helpful?

Solution

Ended up fixing it myself. My prefs class and sharedpreference manager caller were being used in the same prefs class, so it was getting saved after the instance had been reinitiated.

Adding

getPrefs inside onSharedPreferenceChanged fixed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top