首页 > 其他分享 >SharedPreferences对数据的存储

SharedPreferences对数据的存储

时间:2022-12-16 17:39:02浏览次数:48  
标签:存储 String Editor editor SharedPreferences commit 数据 preferences


SharedPreferences简介:                                                                         

它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息,比如窗口状态,一些小型自定义数据等。其存储位置在/data/data/<包名>/shared_prefs目录下,可以通过DDMS--FileExplorer下查看(选中文件点击DDMS右上角的导出文件)。

   数据的存储:                                                                                             

   1.构造函数:   

如示例,一般常用的构造函数为2个参数

第一个为sp的名称,第二个一般为权限比如Activity.MODE_PRIVATE, MODE_APPEND, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE

​​public​​          ​​class​​          ​​Calc extends Activity {​​         


​​public​​ ​​static​​ ​​final String PREFS_NAME = ​​ ​​"MyPrefsFile"​​ ​​;​​





​​@Override​​


​​protected​​ ​​void​​ ​​onCreate(Bundle state){​​


​​super.onCreate(state);​​


​​. . .​​





​​// Restore preferences​​


​​SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);​​


​​boolean silent = settings.getBoolean(​​ ​​"silentMode"​​ ​​, ​​ ​​false​​ ​​);​​


​​setSilent(silent);​​


​​}​​



  

   2.数据的存储以及修改:   

使用SharedPreferences.Editor类来构造一个编辑器进行存储数据

​​add.setOnClickListener(​​          ​​new​​           ​​OnClickListener(){​​          


​​@Override​​


​​public​​ ​​void​​ ​​onClick(View v) {​​


​​// TODO Auto-generated method stub​​


​​String str_name = (String) name.getText();​​


​​String str_path = (String) text.getText();​​


​​SharedPreferences Addresses = getSharedPreferences(PREFS_NAME,Activity.M );​​


​​SharedPreferences.Editor editor = Addresses.edit();​​ ​​//通过SharedPreferences.edit()来对Editor进行初始化​​


​​editor.putString(str_name,str_path);​​          ​​//添加数据 <br>                                                            editor.commit(); //数据添加后必须提交才会修改xml文件 <br>                               Toast.makeText(getApplicationContext(), "Successed", Toast.LENGTH_LONG).show(); <br>                         } <br>                        });​​



删除数据


​​public​​          ​​boolean onItemLongClick(AdapterView<?> parent, View view,​​         


​​int​​ ​​position, ​​ ​​long​​ ​​id) {​​


​​SharedPreferences Addresses = getSharedPreferences(PREFS_NAME,​​


​​Activity.MODE_PRIVATE);​​


​​SharedPreferences.Editor editor = Addresses.edit(); ​​ ​​//通过SharedPreferences.edit()来对Editor进行初始化​​


​​editor.​​ ​​remove​​ ​​((String) listview.getSelectedItem()); ​​ ​​//删除相应的数据​​


​​editor.commit(); ​​ ​​//同样需要提交​​


​​}​​





​​});​​



   3.一些常用的方法:   

void apply()


Commit your preferences changes back from this Editor to the  ​​SharedPreferences​​ object it is editing.

Mark in the editor to remove all values from the preferences.

boolean commit()


Commit your preferences changes back from this Editor to the  ​​SharedPreferences​​ object it is editing.

Set a boolean value in the preferences editor, to be written back once  ​​commit()​​ or  ​​apply()​​ are called.

Set an int value in the preferences editor, to be written back once  ​​commit()​​ or  ​​apply()​​ are called.

Set a String value in the preferences editor, to be written back once  ​​commit()​​ or  ​​apply()​​ are called.

Set a set of String values in the preferences editor, to be written back once  ​​commit()​​ is called.

Mark in the editor that a preference value should be removed, which will be done in the actual preferences once  ​​commit()​​ is called.

 

   数据的读取:                                                                                            



SharedPreferences.Editor edit()


Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.

Map<String, ?> getAll()


Retrieve all values from the preferences.

boolean getBoolean(String key, boolean defValue)


Retrieve a boolean value from the preferences.

Retrieve a String value from the preferences.

Retrieve a set of String values from the preferences.

void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)


Registers a callback to be invoked when a change happens to a preference.

void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)


Unregisters a previous callback.



 

   使用getAll()读取所有数据存储在一个HashMap中:

​​listview = (ListView) findViewById(R.id.lv);​​          


​​SharedPreferences Addresses = getSharedPreferences(PREFS_NAME, 0);​​


​​listItems = (HashMap<String, Object>) Addresses.getAll();​​



  还是挺好用的哈~~

标签:存储,String,Editor,editor,SharedPreferences,commit,数据,preferences
From: https://blog.51cto.com/u_3457306/5948002

相关文章