im trying to retrieve the values but somehow i get alot more values back. this is the code im running. Anyone knows what im doing wrong? i did search at alot of places but most is outdated. ( i checked item 2 and 3) Just to make sure i only want to read out the selected checkboxes of the list but i have no idea where the 0-1-2-123 come from.
pref_app.xml
<MultiSelectListPreference
android:title="Categories"
android:key="rssfeeds"
android:summary="List to choose from"
android:entries="@array/catos"
android:entryValues="@array/catovalues"
android:defaultValue="@array/catodefault"
android:dialogTitle="Categories"
>
</MultiSelectListPreference>
Strings.xml
<string-array name="catos">
<item >Movies</item>
<item >Test</item>
<item >TEst23</item>
</string-array>
<string-array name="catovalues">
<item >movies-checked</item>
<item >test-checked</item>
<item >test23-checked</item>
</string-array>
<string-array name="catodefault">
<item >movies-default</item>
<item >test-default</item>
<item >TEST23-default</item>
</string-array>
return code in main xml
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Set<String> selections = preferences.getStringSet("rssfeeds", null);
String[] selected= selections.toArray(new String[] {});
for (int i = 0; i < selected.length ; i++){
System.out.println("\ntest" + i +" : " + selected[i]);
}
Logcat result:
01-15 14:15:49.016: I/System.out(4555): test0 : test23-checked
01-15 14:15:49.016: I/System.out(4555): test1 : 2
01-15 14:15:49.016: I/System.out(4555): test2 : 1
01-15 14:15:49.016: I/System.out(4555): test3 : 0
01-15 14:15:49.016: I/System.out(4555): test4 : 123
01-15 14:15:49.016: I/System.out(4555): test5 : test-checked
It stores only the ones that are checked.
You can put values as indices.
<string-array name="catos">
<item >Movies</item>
<item >Test</item>
<item >TEst23</item>
</string-array>
<string-array name="catovalues">
<item >0</item>
<item >1</item>
<item >2</item>
</string-array>
0 - Movies => Checked
1 - Test => Unchecked
2 - TEst23 => Checked
In the preferences file will only indexes 0 and 2, and you can assume that others are unmarked.
So if you analyze the Set <String>, its size will be only 2 strings with 0 and 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With