Android learning notes-how to save data 2-SharedPreferences

Source: Internet
Author: User

Android learning notes-how to save data 2-SharedPreferences
In Android, the data is saved. We have discussed how to save the data to the memory and SD card. This time, we will introduce how to save the data using SharedPreferences. 1, get the SharedPreferences object. 2. Use SharedPreferences to get the editor, that is, Edit (essentially a map). 3. Put the data in it. Remember this transaction when it is finished, remember to submit commit. Public static void saveUserInfo (Context context, String username, String password) {SharedPreferences sp = context. getSharedPreferences ("config", Context. MODE_PRIVATE); // get the Editor edit = sp of the sp. edit (); edit. putString ("username", username); edit. putString ("password", password); // similar to the database, ensure that the data is submitted successfully at the same time. edit. commit (); after the data is saved, how can we read the data? In fact, the data just merged is an xml file, its location is/data/package name/shared_prefs (new)/. Here is the program After the xml Path is generated, the SharedPreferences object is obtained first, and then the corresponding ELE. Me value can be obtained through the key, which is 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 );

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.