Directory (?) [-]
- XML file
- Save in Device
- Read the information
Listpreference provides a single-selection list, which we can provide through checkboxpreference. In addition, Android provides multiselectlistpreference after 3.0, its implementation and listpreference similar, the difference is also can not select or multiple choices.
XML file
Add the relevant content to the composite example as follows. Similarly, @array can be used to define entry and Entryvalue, which is easier to use than a checkbox preference.
......
<preferencescreen android:key= "Multi_select_test"
android:title= "MultiSelect List Preference"
android:summary= "Test for MultiSelect List Preferences" >
<multiselectlistpreference android:key= "Fruits"
Android:title= "Select Your favourite Fruits"
android:summary= "Choose your favorite Fruit" android:entries= "@array/fruit"
android:entryvalues= "@array/fruit_index" android:dialogtitle= "Fruit:"/>
</PreferenceScreen>
... ... |
Save in Device
<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>
<map>
... ...
<set name= "Fruits" >
<string>2</string>
<string>0</string>
</set>
... ...
</map>
Read the information
As can be seen from the save file, the data format is set, reading is slightly more troublesome than before, the code is as follows:
Sharedpreferences prefs = preferencemanager.getdefaultsharedpreferences (this);
set<string>options = prefs.getstringset ("fruits", null); Null is determined because the initial value is not set
Showinfo ("Multi Select:" + options);
string[] Fruits = getresources (). Getstringarray (R.array.fruit);
if (options! = null) {
For (String f:options) {
Showinfo ("select" + Fruits[integer.parseint (f)]);
}
}
The example code covered in this blog post can be downloaded in the Pro Android Learning: Preference (Preferences) Small example.
RELATED Links: My Android development related articles
"Turn" Pro Android Learning Note (60): Preferences (4): MultiSelect List Preference