ANDROID_ Persistence Technology

Source: Internet
Author: User
Tags sqlite database

----Sharedpreferences of persistent technology

The first time to learn sharedpreferences when the head turned not bend, and then his own research to understand, the impression is particularly deep, hope to write this can help, less detours.

1 concept: Storage of some data can be achieved at some time to remove the desired data. Better apps need to save user settings, such as default font/size information, cache some network data, cache images to files, and more.

2 Implementation mode:1. File (I/O stream), such as saving a network image 2.SQLite database, such as saving network data 3.SharedPreferences, such as the configuration information of the app 4.ContentProvider 5. Network.

3 Storage methods: file storage, sharedpreferences storage and database storage

4SharedPreferences: A stored step in the form of a key-value pair:

a first gets the Sharedpreferences object Sharedpreferences Shorthand =SP

(There are two parameters in Getsharedpreferences, parameter 1 SP name, parameter 2 access permission//getpreferences only one parameter, parameter is access permission, this method default current activity);

Example of storing data in B to sharedpreferences: Sharedpreferences.editor Editor = Shared.edit ();

(the editor's commit or apply method will be effective)

c Get data from Sharedpreferences extract the value of the object type from the SP file by the Get method

Parameter 1key; Parameter 2value;

Special Note: When the corresponding data cannot be fetched through key, the result is the default value (the default value is the value corresponding to get);

For example: When storing data: editor.putstring ("name", "Jahaijan");

When fetching data: String name = shared.getstring ("name", "fool"); ---because the name is stored before the key, store name corresponding value is Jahaijan, so the data obtained is ("name", "Jahaijan") instead ("Name", "Fool")

Example 2: When storing data: editor.putstring ("name", "Jahaijan");

When fetching data: String name = shared.getstring ("Nam", "fool"); --when getting data, because of the wrong writing key, and did not have NAM this key, must not be able to retrieve the corresponding value. So the result is ("Nam", "fool");

Special Note: When the corresponding data cannot be fetched through key, the result is the default value (the default value is the value corresponding to get);

What is the default value: When fetching data: String name = shared.getstring ("Nam", "fool"); --The Fool here is the default value

--------------------------------------------------------------------------------------------------------------- ---------------------------------

 mainactivity .

Public class Mainactivity extends Appcompatactivity implements View.onclicklistener {

private TextView TextView;
Private Button save, get;
private String name;
private sharedpreferences gkfx;


@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
//Use sharedpreferences steps

TextView = (textView) Findviewbyid (r.id.rv_show);
save = (Button) Findviewbyid (r.id.btn_save);
get = (Button) Findviewbyid (r.id.btn_get);
Save.setonclicklistener (this);
Get.setonclicklistener (this);
//1 getting sharedpreferences objects
//Parameter 1 SP's name. Parameter 2 SP access rights.
//need to be aware of naming conventions for file names
name = "Com.example.SETTING";
/////Parameter 2 permissions to fill in the private, meaning; The file can only be accessed by this app
shared = getsharedpreferences (name, mode_private);
/** The second method of getting an object, the default current activity
* gkfx = getpreferences (mode_private);
*/

}

@Override
Public void OnClick (view view) {
switch (View.getid ()) {

Case R.id.btn_save:
SaveData ();
Break ;

Case R.id.btn_get:
GetData ();
Break ;

}

}

private void SaveData () {
//Store data to Sharedpreferences
//2. Editing an SP file through the editor object
sharedpreferences.editor Editor = Shared.edit ();
//sp file is a key-value pair (Key,value) to save the data
editor.putstring ("name", "Long Green");
editor.putstring ("Like", "Inflatable Doll");
Editor.putint ("Time", 1);
//3. The commit method that needs to invoke editor can take effect, or apply to issue, submit changes to take effect
editor.apply ();


}


Public void GetData () {
//Remove files from SP
//Remove the value of the object type from the SP file by the Get method
//Parameter 1 key parameter 2 default value. When the corresponding data cannot be fetched by key, the result is the default value
String name = shared.getstring ("name", "Tao");
String like = shared.getstring ("Like", "Huqingke");
int time = Shared.getint ("Time", +);

Textview.settext (name + "likes" + Like + "finished" + Time + "Times");

}
}
----------------------------------------------------------------------------------------------------------- ------
activity_main.xml File

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:tools= "Http://schemas.android.com/tools"
android:orientation= "Vertical"
android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "com.example.dllo.a12_sp. Mainactivity ">

<textview
android:id= "@+id/rv_show"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Hello moto!"/>


<button
android:text= "Save Data"
android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:id= "@+id/btn_save"/>


<button
android:text= "Fetch data"
android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:id= "@+id/btn_get"/>
</LinearLayout>

ANDROID_ Persistence Technology

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.