简体   繁体   中英

Android Get Keys from preferences.xml

I have a PreferencesActivity that shows a preferences.xml with checkboxes.

preferences.xml :

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Keywords">
    <CheckBoxPreference android:key="Essen" 
                        android:title="Essen" 
                        android:selectable="true" 
                        android:enabled="true"
                        android:persistent="false">
    </CheckBoxPreference>
    <CheckBoxPreference android:key="Kleidung" 
                        android:title="Kleidung" 
                        android:selectable="true" 
                        android:enabled="true"
                        android:persistent="false">
    </CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>

PreferencesActivity:

public class PreferencesViewController extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

Now in a different ListActivity I want to show all Keys/Titles from the checked checkboxes.

I try to access the Preferences with

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

or

SharedPreferences prefs = getSharedPreferences("mypackage_preferences", 0);

But both dont really work.

When I call prefs.getAll().size() the result is 0.

I can access the Keys/Title with getPreferenceScreen().getPreference(i).… but it doesn't work from a different Activity , only from the PreferenceActivity .

Does anybody have a solution how to get this work?

You don't need a PreferenceActivity to accomplish this.

To access the preferences, that are used in your PreferenceActivity, you should use

SharedPreferences prefs = 
    PreferenceManager.getDefaultSharedPreferences(getBaseContext());

If your list of data comes from the server, than you can use a ListActivity / ExpandableListActivity / any custom activity to visualize it, but this way you need to write the handlers that change the preferences.

The common way to do this would be:

private void saveStringPreference(final String key, final String value)
{
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(key, value);
    editor.commit();
}

If you need, you should create similar wrappers to deal with int, boolean, etc. values.

I had this problem too. This is a classic RTFM, sadly. You should put the line below in your MainActivity's onCreate() method:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

This info can be found in the paragraph "Setting Default Values" of http://developer.android.com/guide/topics/ui/settings.html

I use

SharedPreferences prefs = 
    PreferenceManager.getDefaultSharedPreferences(getBaseContext());

And then I can access the preferences via prefs.get...() , eg prefs.getString(key) . Have you tried this?

Edit: Just checked - prefs.getAll() works as expected and returns a Map with all preferences.

You should use

SharedPreferences prefs = referenceManager.getDefaultSharedPreferences(this);

The preferences may be empty if you have never set them via the PreferencesActivity. Also I think your ListActivity has to be in the same package as the PreferencesActivity.

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