Reputation: 44308
I have some list preferences, but I don't know how to save the individual values from the list. How do I do it? Here is what I have
http://i41.tinypic.com/dh4gvo.png
Preference customPref = (Preference) findPreference("notificationPref");
customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
SharedPreferences customSharedPreference = getSharedPreferences(
"notifications", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = customSharedPreference
.edit();
editor.putString("notification",
"The preference has been clicked");
editor.commit();
return true;
}
});
my list click listener is only for the main item in the list preferences page, but not the items in the popup itself. How do I save the choice selected in the popup itself?
Upvotes: 1
Views: 2652
Reputation: 5750
You can read preferences like this...
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString("key", "Default Value");
Upvotes: 2
Reputation: 491
This is usually automatic. In your preference screen XML, you should have something like this:
<ListPreference android:title="@string/Title"
android:summary="@string/Summary"
android:key="PreferenceKey"
android:defaultValue="VALUE_2"
android:entries="@array/Entries"
android:entryValues="@array/Values" />
And in your strings.xml:
<string name="Value1">Text for value 1</string>
<string name="Value2">Text for value 2</string>
<string name="Value3">Text for value 3</string>
<string-array name="Entries">
<item>@string/Value1</item>
<item>@string/Value2</item>
<item>@string/Value2</item>
</string-array>
<string-array name="Values">
<item>VALUE_1</item>
<item>VALUE_2</item>
<item>VALUE_3</item>
</string-array>
The "Values" array specify the (string) value saved in preferences, whereas the "Entries" array specify the text of the items displayed to the user. Each time the user select an item, its corresponding value in the "Values" array is saved to preferences under the specified key ("PreferenceKey" in this example).
Upvotes: 4