shared preferences in android
how to strore d开发者_如何学Pythonata in shared preferences in table format
For store the values using shared preference use below code
SharedPreferences prefs=getSharedPreferences("Key", 0);
Editor e= prefs.edit();
e.putString("Name", "AAA");
e.commit();
For retrieve the shared prefs value use below code
SharedPreferences prefs=getSharedPreferences("Key", 0);
String s= prefs.getString("Name", "");
I don't think you can save table in sharedpreferences; it is saved as like key-value pair.
You can serialize your object and save it in the preferences. Use Serializable or JSON or Protocol Buffers or whatever you're comfortable with.
SharedPreferences store primitive data in a file as key-value pairs.
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings.
http://developer.android.com/guide/topics/data/data-storage.html#pref
I adviese to use SQLITE Database to store data in table format.
http://developer.android.com/guide/topics/data/data-storage.html#db
Shared preferences = "Key" , "Value" sets.
If we wants to store small amount of data like usernames, passwords then we will go for Shared Preferences.
To store small amount of data there is no need of creation of Database, tables, insertion query and retrieval query --- So better to go for Shared Preferences.
Shared Preferences is to stores small amount of data, where as SQLite3 is to store large amount of data.
Shared preferences works like HashMap in Collections.
Shared preferences data will be stored in xml file. This xml file we can find in the following location.
Go to
- Open DDMS perspective.
- Select emulator / rooted Device from the left side panel.
- Select File explorer tab from the right side panel.
- Open data folder.
- Again open data folder.
- Open our package name.
- Here you will find the Shared Preference folder.
- Inside this Shared Preference folder - our shared preference xml file will be visible.
- We can pull this xml file from the emulator by selecting the xml file and click on Left arrow on top right side of the DDMS window.
- We can also change values in the xml file, then we can push this changed xml file into emulator by clicking on the Right arrow which is there on top right side of this DDMS window.
Note: When you push anything into emulator "DONT FORGET TO RESTART YOUR EMULATOR". Otherwise the changes will not be effected.
Storing the values into shared preferences
import android.content.SharedPreferences;
SharedPreferences preference;
SharedPreferences.Editor editor;
preference=getApplicationContext().getSharedPreferences("PROFILE", 0);
editor=preference.edit();
editor.putString("MANUALPROFILENAME", newProfileValue);
editor.commit();
For getting the values from the shared preferences
import android.content.SharedPreferences;
SharedPreferences preference;
SharedPreferences.Editor editor;
preference=getBaseContext().getSharedPreferences("PROFILE", 0);
String manualsetunset =preference.getString("MANUALPROFILEENAME", "false");// Here false is default value. If the required string does not found in shared preference, the default value will be stored in the string object.
精彩评论