Pierre Rymiortz
Pierre Rymiortz

Reputation: 1127

How read values from MultipleSelectListPreference

I want to read my string arrays entries from SharedPreferences that were saved via MultipleSelectListPreferences.

getStringSet() seems to be the only method on SharedPreferences that fits the requirements. How do I extract the saved String array with this method - I don't understand how to get the array from a String set.

Upvotes: 1

Views: 88

Answers (1)

Marcin S.
Marcin S.

Reputation: 11191

I have provided a simple example of how to read a MutipleSelectListPreference into set and then, convert it into an String array and save it into a String.

First you read your sharedPreferences into the mySet object:

Set<String> mySet = sharedPreferences.getStringSet('your_preference_key', new Hashset<String>());

Then you can define String array, iterate through the mySet and save each string into one fromSet string:

String fromSet;
String myArray[] = mySet.toArray(new String[mySet.size()]);
for (int index = 0; index < myArray.length; index++) {
    fromSet += myArray[index] + "\n";
}

Upvotes: 1

Related Questions