"Turn" Pro Android Learning Note (60): Preferences (4): MultiSelect List Preference

Source: Internet
Author: User

Directory (?) [-]

    1. XML file
    2. Save in Device
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.