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(); |
还是挺好用的哈~~
相关推荐
android 使用SharedPreferences对数据存储/删除/读取/文件删除
layout9为SharedPreferences数据存储和读取功能应该的一个关于记住密码功能实例。 layout11为SharedPreferences的数据存储和读取实例。
NULL 博文链接:https://hack-zhang.iteye.com/blog/1265370
Android使用SharedPreferences进行数据存储,缓存ID等数据
实验九 使用SharedPreferences存储简单数据
Android 3种数据保存(SharedPreferences存储 内部文件存储 数据库存储),如项目包有问题请联系我谢谢
前面呢,我们讲了文件的存储,文件的存储方式不适合保存一些比较复杂的文本数据。因此,我们来学习一下...SharedPreferences支持不同类型的数据存储,如果你存储的是整型,那你读取出来的也是整型,你存储的是浮点型,
Android项目开发中使用的数据存储方式有:网络存储、sqlite存储、File存储和SharedPreferences存储,四种存储方式对应的Demo别人是NetworkDemo、SqliteDemo、FileDemo和SharedPreferencesDemo,根据应用的场景选择...
Sharedpreferences存储数据的使用方法_完整Demo_AndroidStudio http://blog.csdn.net/iwanghang
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,不同于文件的存储方式,SharedPreferences是使用键值对(key-value)数据的方式来存储数据的。而且SharedPreferences还支持多种不同的数据类型...
本实例通过使用SharedPreferences方式储存网络上的数据,实现在没有网络的情况下也可以显示数据,节省流量的一个好办法!
Android数据存储之SharedPreferences,自己根据文档编写的代码,有需要的就下载吧。
打卡日程,小作业,SharedPreferences存储数据
刘氏一键拨号app[SharedPreferences存储数据]
嵌入式Android项目设计与开发 第六章 数据存储 ——SharedPreferences 2 SharedPreferences 键值对概念: 某一个数据都由一个键和一个值构成,键代表数据的名称,它是全局唯一的。具体的数据内容存放在值中。 键值对...
SharedPreferences数据保存在: /data /data/<package> /shared_prefs 文件夹下,以XML格式保存,根元素为:。文件名称为获取SharedPreferences实例时传递的參数值。 <string name=key> value 获取实例 ...
fristActivity用于存储SharedPreferences数据,secondActivity删除SharedPreferences,moreactivtiy用于判断SharedPreferences是否删除,如果删除跳转到secondActivity,没有跳转到fristActivity。
数据持久化就是指将那些内存中的瞬时数据保存到持久化设备中(如手机文件、数据库等),当关机,...SharedPreferences 是使用键值对的方式来存储数据的,使用 SharedPreferences来进行数据持久化要比使用文件方便很多。
很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为...使用 SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package