Android-sharedpreferences and editor save data

Source: Internet
Author: User


Sharedpreferences is the easiest data storage technology to understand in Android, and in fact sharedpreferences is dealing with a key-value (key-value pair) Sharedpreferences used to store some lightweight data.


In the time to do the game, you need to save the game progress, so a little use a bit, here to do a summary.


My approach is to first give the following definition:

Private sharedpreferences sp;private static String my_app= "MYAPP";

Then write two methods to perform the data read and write operations.

/** * Save data */public void SaveData (String key,int value) {//Get Sharedpreferences Object sp = MainActivity.this.getSharedPreferen Ces (My_app, mode_private);//Deposit data, in addition to Putint and putstring and other methods, you can dig yourself ~//in addition, each time the deposit is "key-value" form of key value pairs, very good understanding editor Editor = Sp.edit (); Editor.putint (key, value); Editor.commit ();} /** * Read data, find returned data content, otherwise return-1 */public int readdata (String key) {sp = MainActivity.this.getSharedPreferences (My_app, Mode_ PRIVATE), int v=sp.getint (key, 0), if (v! = 0) {return v;} else {return-1;}}

These two methods can then be used to access the data in the program. If you want to save a few data items, but more frequently, you can also define the key as a static variable, so that the use of more convenient, and easy to modify.

The use of the Putint and ReadData return value is I according to the needs of their own programs to set, how to write and whether it is necessary to add some constraints, according to their own procedures to make appropriate adjustments, the need for clear, this is also very easy to do.


The following is a complete code

package com.test.leetlesharedpreferences;import android.os.bundle;import android.app.activity; Import android.content.sharedpreferences;import android.content.sharedpreferences.editor;import  android.view.Menu;public class MainActivity extends Activity {private  sharedpreferences sp;private static string my_app= "MYAPP";p rivate static string  my_keyint= "KEYINT";p rivate static string my_keystring= "keystring";      @Override     protected void oncreate (bundle savedinstancestate)   {        super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);                          System.out.println ("ReadInt (my_keyint) &NBsp;=  "+integer.tostring (ReadInt (my_keyint)));         System.out.println ("readString (my_keystring)  = " +readstring (my_keystring));         system.out.println ("--------I am a split line----------------------");                 savedata (MY_KEYINT, 100000);         savedata (my_keystring,  "That's the problem");         system.out.println ("ReadInt (my_keyint)  = " +integer.tostring (ReadInt (my_ KEYINT));         system.out.println ("ReadString (my_keystring)  =   "+readstring (my_keystring));             }      @Override     public boolean oncreateoptionsmenu (Menu  menu)  {    &NBsp;   // inflate the menu; this adds items to the  action bar if it is present.         Getmenuinflater (). Inflate (R.menu.main, menu);        return  true;    }            /**      *  two ways to write data      */    private  void savedata (String key,int value)     {    sp= This.getsharedpreferences (my_app, mode_private);     editor editor=sp.edit ();     editor.putint (Key, value);     editor.commit ();     }    private void savedata (String key,string value)      {     sp=this.getsharedpreferences (my_app, mode_private);     editor editor= Sp.edit ();     editor.putstring (Key, value);     editor.commit ();     }      /**     *  two ways to read data      */    private int readint (String key)      {    sp=mainactivity.this.getsharedpreferences (MY_APP, MODE_PRIVATE );     return sp.getint (key, 0);    }         private string readstring (String key)     {     sp=this.getsharedpreferences (my_app, mode_private);    return  Sp.getstring (key,  "no Data");     }}

2015.01.02

Li Yengiang


Android-sharedpreferences and editor save data

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.