首页 > 试题广场 >

对于一个已经存在的SharedPreferences对象se

[单选题]
对于一个已经存在的SharedPreferences对象setting,想向其中存入一个字符串"person",setting应该先调用什么方法( ).
  • edit()
  • save()
  • commit()
  • putString()
A
--------------------
//实例化SharedPreferences对象(第一步)
SharedPreferences mySharedPreferences= getSharedPreferences("test", 
Activity.MODE_PRIVATE); 
//实例化SharedPreferences.Editor对象(第二步)(android.content.SharedPreferences.Editor
)
SharedPreferences.Editor editor = mySharedPreferences.edit(); 
//用putString的方法保存数据
editor.putString("name", "Karl"); 
editor.putString("habit", "sleep"); 
//提交当前数据
editor.commit(); 
编辑于 2015-05-16 12:08:33 回复(3)

SharedPreference

  1. 是一种轻型的数据存储方式
  2. 本质是基于XML文件存储key-value键值对数据
  3. 通常用来存储一些简单的配置信息 
1
2
3
4
5
6
7
8
9
10
//获取实例
SharedPreferences pref = getSharedPreference("myPref",MODE_PRIVATE);
//启动编辑
SharedPreferences.Editor editor = pref.edit();
//写数据
editor.putString("name","ws");
//提交
editor.apply();
//读数据
pref.getString("name","");

发表于 2016-03-22 14:47:33 回复(1)
选A。sp的调用顺序adc。
发表于 2015-03-08 20:51:51 回复(0)
apply()和commt()方法的差别在于:
1. apply没有返回值而commit返回boolean表明修改是否提交成功 
2. apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘,因此,在多个并发的提交commit的时候,他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。而apply只是原子的提交到内容,后面有调用apply的函数的将会直接覆盖前面的内存数据,这样从一定程度上提高了很多效率。 
3. apply方法不会提示任何失败的提示。 
由于在一个进程中,sharedPreference是单实例,一般不会出现并发冲突,如果对提交的结果不关心的话,建议使用apply,当然需要确保提交成功且有后续操作的话,还是需要用commit的。
发表于 2018-03-13 15:05:31 回复(0)
这下阴沟里翻船了,即使曾经写过代码还是很容易会忽略,就是给SharedPreferences对象写入数据之前,先要调用edit()方法来获取编辑对象。从读取 SharedPreferences对象读取数据时直接读取
发表于 2015-09-24 15:38:12 回复(1)
第一步edit
第二步putSrring()
第三步commit()
发表于 2019-08-06 18:57:04 回复(0)
//实例化SharedPreferences对象
SharedPreferences myPren=getSharedPreferences("myPren",Activity.MODE_PRIVATE);
//第二步实例化SharedPreferences.Editor对象
SharedPrerences.Editor editor=myPren.edit();
//第三步保存key-values形式的数据
editor.putString("key","person");
//最后一步提交数据
editor.commit();
发表于 2018-01-10 20:34:57 回复(0)
SharePreference保存数据都是先edit后commit操作
发表于 2017-09-17 20:22:18 回复(0)
这题目有歧义 不过还是选A 没 edit 根本写不了
发表于 2017-04-25 01:26:10 回复(0)

SharedPreference

  1. 是一种轻型的数据存储方式
  2. 本质是基于XML文件存储key-value键值对数据
  3. 通常用来存储一些简单的配置信息 

  4. //获取实例         
  5. SharedPreferences pref =
  6. getSharedPreference("myPref",MODE_PRIVATE);         
  7. //启动编辑         
  8. SharedPreferences.Editor editor = pref.edit();         
  9. //写数据         
  10. editor.putString("name","ws");         
  11. //提交         
  12. editor.apply();          
  13. //读数据         
  14. pref.getString("name","");         


发表于 2016-11-27 11:00:21 回复(0)
http://www.jb51.net/article/31911.htm

发表于 2015-08-18 09:38:30 回复(0)
A
发表于 2015-04-05 17:59:09 回复(0)
A 先编辑然后才能操作
发表于 2015-04-03 14:33:04 回复(0)
实例化SharedPreferences.Editor对象
发表于 2015-03-27 14:13:56 回复(0)
A
发表于 2015-03-12 10:21:55 回复(0)
A
先调用edit,再调用putString,最后调用commit
发表于 2015-03-06 14:32:05 回复(0)
A
需要调用edit方法
编辑于 2015-03-04 11:35:35 回复(0)
D
发表于 2015-02-28 14:22:32 回复(0)
A
发表于 2015-02-11 22:13:45 回复(0)