How to store data under Android

Source: Internet
Author: User

The Android system provides several ways to store data by default:

    1. Shared Preferences
    2. Internal storage
    3. External storage
    4. SQLite database
    5. Save to network server

Using the shared Preferences

The Shared preferences class is primarily used to hold the data type of a key-value pair. We can use it to save some simple data types.

There are two ways to get sharedpreferences objects:

    1. Through the Getsharedpreferences () method
    2. Through the Getpreferences () method

The main differences between the above two methods are:

The first method can create multiple files to hold the data

The second method can only create one file to save data

Let's start by creating a layout like this:

Layout code:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical" ><edittext     android:id= "@+id/username"    android:layout_width= " Fill_parent "    android:layout_height=" wrap_content "/><edittext     android:id=" @+id/password    " Android:layout_width= "Fill_parent"    android:layout_height= "wrap_content"/><button     android:id= "@+ Id/write "    android:layout_width=" wrap_content "    android:layout_height=" Wrap_content "    android: Layout_gravity= "right"    android:text= "write to File"/>    </LinearLayout>

Once the layout is ready, you can get the Sharedpreferences object to write to the data:

Package Com.whathecode.storageoptinos;import Android.app.activity;import Android.content.sharedpreferences;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.EditText;import Android.widget.toast;public class Mainactivity extends activity{@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); final EditText Namefield = (EditText) Findviewbyid (r.id.username); final EditText Agefield = (EditText) Findviewbyid (r.id.age); Button Btnwrite = (button) Findviewbyid (r.id.write);//Get Sharedpreferences object, file permission is private sharedpreferences SPF = Getsharedpreferences ("Record", mode_private);//Get editor final Sharedpreferences.editor editor = Spf.edit (); Btnwrite.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {String username = Namefield.gettext (). toString (); String age = Agefield.gettext (). toString (); Editor.putstring ("username", username); Editor.putstring ("Age", age), if (Editor.commit ())//When the data is successfully written back True{toast.maketext (Getbasecontext (), "file written", Toast.length_short). Show ();}}});}}

In the above code, we mainly use Sharedpreferences's internal class editor class

We mainly use several put* methods of this class

When these methods are called, we can start writing the data using the commit () or the Apply () method.

It is important to note that when multiple editor writes data to the same file, the result of an editor after completion overwrites the previous one.

The difference between the commit and the Apply method is that the commit write successfully returns TRUE, and apply does not return a value.

You can call the Apply () method to write the data when we don't care about the return value.

Operation Result:

After the data is written, we can see if there is a write success in Eclipse's DDMS attempt

The data is saved in the/data/data/package name/shared_prefs Directory

How to store data under Android

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.