7

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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
liquid
  • 71
  • 1
  • 1
  • 2

4 Answers4

3

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.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
2

I use

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

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

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

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • The Problem is that I want to update these preferences from Internet/Server. So i dont know the keys for every checkbox. So the Preferences should be shown in an Activity/View, the User can check/uncheck them. I have to read the checked preferences and the preferences should be updated by a server… maybe the PreferenceActivity is not the right solution?! :-/ – liquid Mar 10 '11 at 09:27
1

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

Victor Ionescu
  • 1,967
  • 2
  • 21
  • 24
0

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.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119