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