Four ways to store Android data sharedpreferences

Source: Internet
Author: User
Tags sqlite database

In addition to the SQLite database, Sharedpreferences is also a lightweight way of storing data, which is essentially based on XML files that store Key-value key-value pairs of data and are typically used to store some simple configuration information. Its storage location is under the/data/data/< >/shared_prefs directory. The Sharedpreferences object itself can only fetch data without supporting storage and modification, and storage modifications are implemented through the editor object. The steps to implement Sharedpreferences storage are as follows:

First, according to the context to obtain Sharedpreferences object

Second, use the edit () method to get the editor object.

Third, through the editor object store Key-value key value pair data.

Iv. submit data through the commit () method.

Here is a case of storing data with sharedpreferences:

Click on to turn on WiFi, set the progress bar, save the state, the next time you open this program, the last saved state will be displayed.

The core code is as follows:

Package Com.ghsy.demo;import Android.os.bundle;import Android.app.activity;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.view.Menu; Import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.seekbar;import Android.widget.seekbar.onseekbarchangelistener;public class Mainactivity extends Activity {private checkbox checkbox; Private SeekBar SeekBar; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); checkbox= (CheckBox) This.findviewbyid (R.id.checkBox1 ); seekbar= (SeekBar) This.findviewbyid (R.ID.SEEKBAR1); * * In addition to the SQLite database, Sharedpreferences is also a lightweight way of data storage, its essence is based on the XML file * Store Key-value Key-value pairs of data, typically used to store some simple configuration information. Its storage location is under the/data/data/< >/sh *ared_prefs directory. The Sharedpreferences object itself can only fetch data without supporting storage and modification, and storage modifications are implemented by the editor object. *///sharedpreferences to store data://1. Get Sharedpreferences Object Final from context Sharedpreferences preferences=mainactivity.this.getsharedpreferences ("setting", mode_private);//Click when the state changes, Call the appropriate method Checkbox.setoncheckedchangelistener (new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged ( Compoundbutton Buttonview,boolean isChecked) {//2. Get Editor objects (objects modified to data store) by Sharedpreferences editor editor= Preferences.edit ();//3. Stores Key-value key values through the editor object to the data Editor.putboolean ("WiFi", isChecked);//4. Submit data via Commit method Editor.commit ();}}); /When the current page is opened, get the current WiFi on status from preferences Boolean boolean1 = Preferences.getboolean ("WiFi", false); Checkbox.setchecked (boolean1);//When SeekBar is moving, make a monitoring this.seekBar.setOnSeekBarChangeListener (new Onseekbarchangelistener () {@Overridepublic void Onstoptrackingtouch (SeekBar SeekBar) {//When the progress bar is stopped} @Overridepublic void Onstarttrackingtouch (SeekBar SeekBar) {//progress bar started to move} @Overridepublic void Onprogresschanged (SeekBar SeekBar, int Progress,boolean fromuser) {//progress bar after movement (in motion) Editor editor=preferences.edit (); Editor.putint ("Seek", progress); Editor.commit ();}}); INT Progress=preferences.getint ("seek", 0); this.seekBar.setProgress (progress);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


 

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.