SharedPreferences对数据的存储

SharedPreferences简介:                                                                         

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

   数据的存储:                                                                                             

   1.构造函数:   

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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类来构造一个编辑器进行存储数据

1
2
3
4
5
6
7
8
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进行初始化
1
editor.putString(str_name,str_path); //添加数据 <br>                                                            editor.commit(); //数据添加后必须提交才会修改xml文件 <br>                               Toast.makeText(getApplicationContext(), "Successed", Toast.LENGTH_LONG).show(); <br>                         } <br>                        });

删除数据

1
2
3
4
5
6
7
8
9
10
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.
 SharedPreferences.Editor clear()
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.
 SharedPreferences.Editorput Boolean(String key, boolean value)
Set a boolean value in the preferences editor, to be written back once  commit() or  apply() are called.
 SharedPreferences.Editor putInt(String key, int value)
Set an int value in the preferences editor, to be written back once  commit() or  apply() are called.
 SharedPreferences.Editor putString(String key, String value)
Set a String value in the preferences editor, to be written back once  commit() or  apply() are called.
 SharedPreferences.Editor putStringSet(String key, Set<String> values)
Set a set of String values in the preferences editor, to be written back once  commit() is called.
 SharedPreferences.Editor remove(String key)
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.
 String getString(String key, String defValue)
Retrieve a String value from the preferences.
 Set<String> getStringSet(String key, Set<String> defValues)
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中:

1
2
3
         listview = (ListView) findViewById(R.id.lv);
SharedPreferences Addresses = getSharedPreferences(PREFS_NAME, 0);
listItems = (HashMap<String, Object>) Addresses.getAll();

  还是挺好用的哈~~

猜你喜欢

转载自duchengjiu.iteye.com/blog/2068024