Android data storage method: SharePreference
Development cannot leave the data, because programming = Algorithm + data. There are five common data storage methods for Android Application Development:
1. Use SharedPreferences to store data 2, file storage data 3, SQLite database to store data 4, and ContentProvider to store data 5. The first storage method for network storage data is briefly described today: use SharedPreferences to store data. --------------------------- Use SharedPreferences to store data -----------------------------------------------------
SharePreference is a lightweight storage mechanism. Only some basic types can be stored, with xml files as the carrier. The file storage path is data/package name/pai_prefs/file name. xml, which is similar to Map and key-Value pairs. When storing data, you need to call an editor attribute of the SharePreference interface to add or remove data through the editor, and you must call the editor's commit method.
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/LinearLayout1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
Android: id = "@ + id/input_edt"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: hint = "Enter the saved content"/>
Android: id = "@ + id/save_btn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "save data"/>
---------------- Data Access Operations ----------------------------------
Public class MainActivity extends Activity implements OnClickListener {
/** Content input box */
Private EditText inputEdt;
/** Save button */
Private Button saveBtn;
/** SharedPreferences */
Private SharedPreferences mSharedPreferences;
/** Editor */
Private Editor mEditor;
Private static final String SAVE_FILE_NAME = "save_spref ";
Private static final String SAVE_FILE_KEY = "save_key ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
FindViewsById ();
MSharedPreferences = getSharedPreferences (SAVE_FILE_NAME, MODE_PRIVATE );
MEditor = mSharedPreferences. edit ();
/** If you want to obtain the corresponding value */
String getContent = mSharedPreferences. getString (SAVE_FILE_KEY ,"");
InputEdt. setText (getContent );
}
Private void findViewsById (){
InputEdt = (EditText) findViewById (R. id. input_edt );
SaveBtn = (Button) findViewById (R. id. save_btn );
SaveBtn. setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
If (v. getId () = R. id. save_btn ){
String content = inputEdt. getText (). toString ();
MEditor. putString (SAVE_FILE_KEY, content );
}
}
/**
* MSharedPreferences = getSharedPreferences (SAVE_FILE_NAME, MODE_PRIVATE );//
The first parameter of the method is used to specify the name of the file without a suffix. The suffix is automatically added by Android;
The second parameter of the method specifies the file operation mode. There are four operation modes.
The four operation modes are as follows:
1. MODE_APPEND: append Storage
2. MODE_PRIVATE: private storage, which cannot be accessed by other applications
3. MODE_WORLD_READABLE: indicates that the current file can be read by other applications.
4. MODE_WORLD_WRITEABLE: indicates that the current file can be written by other applications.
*/
}