简体   繁体   中英

ListPreference only returns Default Value

I have setup ability for a user to specify some settings using the in-built preference system. My preference.xml is simple, with only a ListPreference:

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

    <PreferenceCategory android:title="Your nuSTOCK Settings" android:persistent="true">

    <ListPreference android:key="operation_section" android:entries="@array/array_nustock_section_values" android:summary="What's your operational section in nuSTOCK?" android:entryValues="@array/array_nustock_section_keys" android:title="Operation Section" android:negativeButtonText="Cancel" android:positiveButtonText="OK" android:persistent="true" android:enabled="true"/>

    </PreferenceCategory>

</PreferenceScreen>

That references my Arrays, which are:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="array_nustock_section_keys">
        <item>store</item>
        <item>branch</item>
    </string-array>
    <string-array name="array_nustock_section_values">
        <item>Store</item>
        <item>Branch</item>
    </string-array>

</resources>

And then I load it (the Preference Module) into my Activity Like this:

nustock_preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

After which I then prompt the user to set the values (select from only two options), by invoking the preference activity via an Intent:

Intent settingsActivity = new Intent(this,
                    MyPreferenceActivity.class);
            startActivity(settingsActivity);

The Preference Activity is like so:

public class MyPreferenceActivity extends PreferenceActivity {



    private static final String PREF_FILENAME = "nustock_preferences";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getPreferenceManager().setSharedPreferencesName(PREF_FILENAME);
            addPreferencesFromResource(R.xml.preferences);            

    }
}

And I then try to read the preference value set by the user like this:

nustock_preferences = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        String op = nustock_preferences.getString(PREF_OPERATION_SECTION,"none");
        Log.d(Tag,String.format("Operation Section : %s", op));

PROBLEM:

No matter what value of the preference I select, only value I get is the default "none" (which I've actually added as different from the actual values in the list, just to highlight the problem -- selected value never gets returned!).

So, What Am I doing wrong? I've tried many variations of this approach, but I can't get the user's selected preference! Even tried restarting the app (hoping that the preferences get set at start-up, nothing!)

But, interestingly, whenever I load the preference screen, the correct value is still selected under the ListPreference dialog!

I believe you specify a particular preference file name with getPreferenceManager().setSharedPreferencesName(PREF_FILENAME); but later you are trying to get preference value from default preferences .getDefaultSharedPreferences(getBaseContext());

It's like writing data to table PERSON, but later trying to find it in the table DEFAULT

Either remove the setting for preference file name, or get your value from the preference file you specified

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM