Android Saving Data (i)

Source: Internet
Author: User

Saving Key-value sets Save key value pair

SharedPreferences只能用来保存一些简单的数据,并且这些数据可以是共享的,也可以是私有的。

SharedPreferences没有构造方法,只能同个Context中的getSharePreference获得。

Get a handle to a shared preference item

You can create a new shared preference file or access an existing file by calling one of the following two methods:

    • getSharedPreferences()-Use this method if you need to identify multiple shared preference files according to the name that you specified with the first parameter. You can call this method from any of your apps Context .
    • getPreferences()-If you only need to use one of the Activity's sharing preferences, Activity Use this method from within. Because this method retrieves the default share preference file that is part of the Activity, you do not need to provide a name.

For example, the following code is Fragment executed inside. It accesses R.string.preference_file_key the shared preference file identified through the resource string and opens it using private mode, allowing only your app to access the file.

For example, the following code executes within Fragment . It accesses the shared preference file identified by the resource string r.string.preference_file_key and opens it using private mode, allowing only your app to access the file.

Context= getactivity();
Sharedpreferences= context. Getsharedpreferences(
GetString(R. String. Preference_file_key),Context. Mode_private);

When naming your shared preference file, you should use a name that is uniquely recognized for your app, such as"com.example.myapp.PREFERENCE_FILE_KEY"

Or, if you only need a shared preference file for Activity, you can use the getPreferences() method:

Sharedpreferences= getactivity().  Getpreferences(Context.  Mode_private);
Write Sharing preferences

To write to the shared preference file, SharedPreferences create one by calling your call edit() SharedPreferences.Editor .

Pass putInt() putString() The keys and values that you want to write using such as and methods. Then, the call is commit() made to save the changes. For example:

SharedpreferencesSharedpref=Getactivity().Getpreferences (context. Mode_private 
sharedpreferences. Editor editor = Sharedpref. Editor. Putint (getstring (r.< Span class= "KWD" >string. Saved_high_score Newhighscore Editor. Commit
Reading information from a shared preference item

To retrieve values from a shared preference file, call methods such as getInt() and, getString() provide a key for the value you want, and optionally provide a default value to return if the key does not exist. For example:

SharedpreferencesSharedpref=Getactivity().Getpreferences(Context.Mode_private);
int DefaultValue = getresources(). Getinteger(R. String. Saved_high_score_default);
long highscore = sharedpref. GetInt(getString(R. String. Saved_high_score), defaultvalue);



Code:
 Public classMainactivityextendsActivity {TextView view; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.fragment_layout); View=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Button Write=(Button) Findviewbyid (R.id.button1); Write.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubSharedpreferences sp=getpreferences (mode_private); Editor Editor=Sp.edit (); Editor.putint (GetString (r.string.flag),1);            Editor.commit ();        }        }); Button Read=(Button) Findviewbyid (R.id.button2); Read.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubSharedpreferences sp=getpreferences (mode_private); intDefalt=-1; intres=Sp.getint (getString (R.string.flag), Defalt);            View.settext (string.valueof (res));                    }        }); }}
View Code

Android Saving Data (i)

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.