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 PreferencesViewControl开发者_开发知识库ler 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 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.
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
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.
精彩评论