Data storage in Android and android

Source: Internet
Author: User

Data storage in Android and android

 

By default, Android provides the following data storage methods:

 

Use Shared Preferences

The Shared Preferences class is mainly used to save the Data Type of key-value pairs. We can use it to save some simple data types.

There are two methods to obtain the SharedPreferences object:

 

The two methods are as follows:

The first method is to create multiple files to save data.

Method 2: only one file can be created to save data.

 

Let's first create a layout like this:

<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>

 

After the layout is ready, you can get the SharedPreferences object to write 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 the SharedPreferences object. The File Permission is private SharedPreferences spf = getSharedPreferences ("record", MODE_PRIVATE); // get the final SharedPreferences editor. 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 () // returns true {Toast. makeText (getBaseContext (), "file written", Toast. LENGTH_SHORT ). show ();}}});}}

In the above Code, we mainly use the internal Editor class of SharedPreferences.

We mainly use several Put * methods of this class.

 

After calling these methods, we can use the commit () or apply () method to start writing data.

Note that when multiple editors write data to the same file, the result of the completed Editor overwrites the previous one.

The difference between the commit and apply methods is that true is returned for successful write of commit, but no return value is returned for apply.

When we don't care about the returned value, we can call the apply () method to write data.

 

Running result:

 

After the data is written, you can check whether the data is successfully written in the ddms attempt of Eclipse.

Data is stored in the/data/package name/shared_prefs directory.

Related Article

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.