Example: How to Use listpreference in Android

Source: Internet
Author: User

First, it is shown as follows:

 

The project code is as follows:

 

Package com. My. listpreference;

Import Android. OS. Bundle;
Import Android. Preference. listpreference;
Import Android. Preference. preference;
Import Android. Preference. Preference. onpreferencechangelistener;
Import Android. Preference. preferenceactivity;

Public class listpreferencetest extends preferenceactivity implements onpreferencechangelistener {
/** Called when the activity is first created .*/

Listpreference LP; // create a listpreference object

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Filter existing XML file resources and add the current preference layer to this preference Layer
Addpreferencesfromresource (R. xml. Preference );
// Initialize this listpreference object
Lp = (listpreference) findpreference (getstring (R. String. key_str ));
// Set the changes in listpreference.
LP. setonpreferencechangelistener (this );
/** Display the abstract content (that is, summary) in listpreference as the value corresponding to the object in the current listpreference.
* This method is used to display the last set Summary (abstract) when the program is opened)
* If this method is not added, when the program is opened again, it will not display the value set for the previous program.
* Display default value */
LP. setsummary (LP. getentry ());
}

// Display the selected items, obtain the changes, and display them.
@ Override
Public Boolean onpreferencechange (preference, object newvalue ){
// Todo auto-generated method stub
If (preference instanceof listpreference ){
// Forcibly convert preference to listpreference type
Listpreference = (listpreference) preference;
// Obtain the entity content in listpreference
Charsequence [] entries = listpreference. getentries ();
// Obtain the object content Base Value in listpreference
Int Index = listpreference. findindexofvalue (string) newvalue );
// Display the abstract in listpreference as the item selected in the Entity content of the current listpreference
Listpreference. setsummary (entries [Index]);
}
Return true;
}
}

 

The content in res. Values. Strings. XML is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>

<String name = "hello"> Hello world, listpreferencetest! </String>
<String name = "app_name"> listpreferencetest </string>
<String name = "key_str"> key </string>
<String name = "title_str"> your favorite vegetables </string>
<String name = "title_listpreference"> select vegetables </string>
<String-array name = "entries_str">
<Item> cabbage </item>
<Item> radish </item>
<Item> bean sprout </item>
<Item> celery </item>
</String-array>
<String-array name = "entries_values_str">
<Item> baicai </item>
<Item> luobu </item>
<Item> douya </item>
<Item> qincai </item>
</String-array>
<String name = "default_str"> baicai </string>
<String name = "dialog_title"> select your favorite vegetables </string>
<String name = "summary_str"> cabbage </string>

</Resources>

 

Res. xml. Preference. xml contains the following content:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Preferencescreen xmlns: Android = "http://schemas.android.com/apk/res/android">
<Preferencecategory Android: Title = "@ string/title_str">
<Listpreference
Android: Key = "@ string/key_str"
Android: Title = "@ string/title_listpreference"
Android: entries = "@ array/entries_str"
Android: entryvalues = "@ array/entries_values_str"
Android: dialogtitle = "@ string/dialog_title"
Android: defaultvalue = "@ string/default_str"
Android: Summary = "@ string/summary_str"
/>
</Preferencecategory>
</Preferencescreen>

 

Note:

 

1.Android: entryvalues = "@ array/entries_values_str" must correspond to Android: entries = "@ array/entries_str"
2.Android: defaultvalue = "@ string/default_str" default_str must be an option in entries_str
3.Lp = (listpreference) findpreference (getstring (R. string. key_str); the content of the obtained string must be key_str; otherwise, the nullpointexception error (that is, the initialization fails) is returned ). Because Android: Key = "@ string/key_str" is equivalent to Android: Id = ""

 

 

 

 

Related Article

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.