Android data Storage (i) Use of preference

Source: Internet
Author: User


Preference provides a lightweight approach to data access, where the application is mostly data-less configuration information. It saves the data in an XML configuration file in the form of a "key • Value" (a Map) pair. For example, we set up a phone boot greeting that can be configured in Preference mode.

Use the preference method to access the data, using the Sharedpreferences interface and an internal interface of the Sharedpreferences Sharedpreferences.editor, both interfaces in the Android.content package.

Call the Context.getsharedpreferences (String name,int mode) method to get the Sharedpreferences interface. The first parameter of the method is the file name, and the second parameter is the operation mode. There are three modes of operation: Mode_private (Private), mode_world_readable (readable), and mode_world_writeable (writable).


Sharedpreferences provides methods for obtaining data , such as GetString (String key,string detvalue), GetInt (String key,int Detvalue) and so on. Calling Sharedpreferences's edit () method returns the sharedpreferences.editor internal interface, which provides methods for saving data , such as putstring (String Key, String value), Putint (string key,int value), and so on, calls the commit () method of the interface to protect the data

Save. Common methods of sharedpreferences and Sharedpreferences.editor are as follows:



sharedpreferences Common Methods


Method Name Method Description


Edit () returns the internal interface of the Sharedpreferences Sharedpreferences.editor

Contains (String key) to determine if the key value is included

GetAll () returns all configuration information Map

Getboolean (String Key,boolean denalue) gets a Boolean value

GetFloat (String key,float Denalue) obtains a float value

GETLNT (String key,int denalue) gets an int value

Getlong (String Key,long denalue) gets a Long value

GetString (String key,string denalue) gets a string value


Sharedpreferences.editor Common Methods


Method Name Method Description


Clear () clears the high value
Commit () Save

GetAll () (Return all configuration information map

Putboolean (String Key,boolean value) holds a Boolean value

Putfloat (String key,float value) holds a float value

PUTLNT (String key,int value) holds an int value

Putlong (String Key,long value) holds a Long value

Putstring (string key,string value) holds a string value

Remove (String key) removes the value corresponding to the key


Use:

Storing data

in void OnStop (), the code is as follows:

protected void OnStop () {super.onstop ();                                                            Sharedpreferences.edit editor = Getshardpreference.edit (String, mode_world_writing).    Edit ();        Editor.putstring ("Key", Myedittext.gettext (). toString ());//Put Data editor.commit (); Save Data}

in void OnCreate (), the code is as follows:

protected void OnCreate () {super.oncreate ();    ......    Sharedpreference pre = Getshardpreference.edit (string,mode_world_writing); Mytextview.settext (Pre.get ("key"));}



This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1618804

Android data Storage (i) Use of 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.