Android Learning notes-How to save data 2-sharedpreferences

Source: Internet
Author: User

Android, the data saved, described earlier, save the data to memory and SD card, this time we will introduce, more commonly use sharedpreferences way to save data,

1, get the object of Sharedpreferences,

2, using sharedpreferences to get the editor, is edit (essentially a map)

3, put the data inside, put out, to remember when a transaction, remember to commit commit.

public static void Saveuserinfo (Context context,string username,string password) {Sharedpreferences sp = Context.getsharedpreferences ("config", context.mode_private);//Get the Editor editor of SP edit = Sp.edit (); Edit.putstring (" Username ", username); edit.putstring (" Password ", password);//similar to the database of things, to ensure that the data submitted successfully edit.commit ();

 

Data saved well, how to read out the data, in fact, just reported that the data, is an XML file, its location is/data/data/package name/shared_prefs (Reborn)/, here is the path of the program to generate XML,

Read the time, is the first to get Sharedpreferences object, and then, through key can get the corresponding hungry value, more convenient.

Sharedpreferences sp = getsharedpreferences ("config", context.mode_private); String username = sp.getstring ("username", "" "); String Password = sp.getstring ("Password", "" "); Et_username.settext (username); et_password.settext (password);

Note: This article to undertake the previous article: http://www.cnblogs.com/fengtengfei/p/3961089.html, contact together to see, the idea is more clear.

Android Learning notes-How to save data 2-sharedpreferences

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.