Sharedpreferences of Android Data storage

Source: Internet
Author: User

Sometimes, applications have a small amount of data that needs to be saved, and the format of the data is simple and is a normal string. The value of scalar type, etc., Android provides sharedpreferences for saving. Sharedpreferences is dealing with a key-value (key-value pair). From a usage point of view, sharedpreferences and sharedpreferences.editor combine very map, where sharedpreferences is responsible for reading data based on key. The sharedpreferences.editor is used to write data.

The following is a simple example to illustrate:

1.XML

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context=". Mainactivity " >Ten  One     <Button A         Android:id= "@+id/button1" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "Save data to Sharedpreferences" /> -  -     <Button -         Android:id= "@+id/button2" + Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:text= "Get data from Sharedpreferences" /> A  at </Relativelayout>

2.java Code:

1  Packagecom.example.sharedpreferences;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 ImportAndroid.content.Context;6 Importandroid.content.SharedPreferences;7 ImportAndroid.view.Menu;8 ImportAndroid.view.View;9 ImportAndroid.view.View.OnClickListener;Ten ImportAndroid.widget.Button; One ImportAndroid.widget.Toast; A  -  Public classMainactivityextendsActivity { -  the     Privatebutton button; -     PrivateButton button2; -  - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); AButton = (Button) This. Findviewbyid (r.id.button1); atButton2 = (Button) This. Findviewbyid (r.id.button2); -Button.setonclicklistener (NewOnclicklistener () { - @Override -              Public voidOnClick (View arg0) { -Savesharpreference (mainactivity. This); -             } in         }); -Button2.setonclicklistener (NewOnclicklistener () { to @Override +              Public voidOnClick (View arg0) { - getsharedpreference (); the             } *         }); $     }Panax Notoginseng  -     //save data to Sharedpreferennes the     Private voidsavesharpreference (Context context) { +Sharedpreferences sharedpreferences = getsharedpreferences ("User", AActivity.mode_private);//instantiating a Sharedpreferences object theSharedpreferences.editor Editor = Sharedpreferences.edit ();//instantiating a Sharedpreferences.editor object +Editor.putstring ("userid", "11"); -Editor.putstring ("username", "Summer"); $ editor.commit (); $Toast.maketext ( This, "data successfully written to sharedpreferences! ", Toast.length_long) - . Show (); -     } the  -     //getting data from SharedpreferencesWuyi     Private voidgetsharedpreference () { theSharedpreferences sharedpreferences = getsharedpreferences ("Test", - activity.mode_private); Wu         //use the GetString method to get value, note that the 2nd parameter is the default value of value -String name = sharedpreferences.getstring ("userid", "" "); AboutString habit = sharedpreferences.getstring ("username", "" "); $Toast.maketext ( This, -"Read the data as follows:" + "\ n" + "name:" + name + "\ n" + "habit:" +habit, - Toast.length_long). Show (); -     } A}

Sharedpreferences of Android Data storage

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.