Android本地最简单的数据存储,没有之一(让SharedPreferences存取JavaBean对象或List)

您所在的位置:网站首页 c4d备份文件存在哪 Android本地最简单的数据存储,没有之一(让SharedPreferences存取JavaBean对象或List)

Android本地最简单的数据存储,没有之一(让SharedPreferences存取JavaBean对象或List)

2023-03-11 09:14| 来源: 网络整理| 查看: 265

不想用SQLite,不想用xml本地存储的同学,SharedPreferences那就简直了,如果要想假装用个数据库,更准确说想用个假数据库,那就不要错过了,SharedPreferences做个小app数据储存,应付毕业设计在笔者看来就足够了。

1.什么是SharedPreferences

我们在开发软件的时候,常需要向用户提供软件参数设置功能,例如我们常用的微信,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是在window下通常我们会采用ini文件进行保存.如果是J2EE下面,我们会采用properties属性文件或者xml进行保存.在我们的Android应用中又适合采用什么方式保存软件配置参数呢? Android平台给我们提供了一个SharedPreferences类,它是一个轻量级应用程序内部轻量级的存储方案,特别适合用于保存软件配置参数,比如boolean,int,float,long,String等数据.使用SharedPreferences以key-value(键值对)的形式进行存储,其实质是采用了xml文件存放数据,路径为:/data/data/< package name>/shared_prefs.

2.SharedPreferences存取对象的思路1.增因为javabean对象是Object类型的,不能直接存储,需要首先先将javabean对象转换成json字符串,再以键值对的方式存储在SharedPreferences中 。也就实现了sp存储对象。如果存储类型是List< bean> ,也把List对象转为json。再以键值对的方式存储在sp中。2.查

从sp中取得json,再由json转换成对象形式。

3.删

如果sp中只存了一个对象,那么可以将整个sp删除,如果存了一个List ,想删除其中某个对象,先将json转换为List对象, 使用list.remove(position) ;方法移除位置为position的对象,再将List重新生成json,最后覆盖存储sp中即可。

4.改

思路同上。

3.实现1. 存储单个JavaBeanSharedPreferences sp = getSharedPreferences("SP_PEOPLE",Activity.MODE_PRIVATE);//创建sp对象,如果有key为"SP_PEOPLE"的sp就取出,否则就创建一个此key的sp对象People people= new People() ;//创建javabean对象people.setId(1); people.setName("小邵");Gson gson = new Gson(); String jsonStr=gson.toJson(people); //将对象转换成Jsoneditor = sp.edit() ;editor.putString("KEY_PEOPLE_DATA", jsonStr) ; //存入json串editor.commit() ; //提交ShowDialog("您已经保存成功"); 2. 存储JavBean的List集合SharedPreferences sp = getSharedPreferences("SP_PEOPLE_List",Activity.MODE_PRIVATE);//创建sp对象List peopleList = new ArrayList() ; //创建List集合对象People people1= new People() ;//创建javabean对象people1.setId(1); people1.setName("小邵"); People people2= new People() ;//创建javabean对象people2.setId(2); people2.setName("小林"); peopleList.add(people1);peopleList.add(people2);Gson gson = new Gson(); String jsonStr=gson.toJson(peopleList); //将List转换成JsonSharedPreferences.Editor editor = sp.edit() ;editor.putString("KEY_PEOPLE_LIST_DATA", jsonStr) ; //存入json串editor.commit() ; //提交ShowDialog("您已经保存成功"); 3. 从SP中查询一个JavaBeanSharedPreferences sp = getSharedPreferences("SP_PEOPLE",Activity.MODE_PRIVATE);//创建sp对象,如果有key为"SP_PEOPLE"的sp就取出peopleJson = sp.getString("KEY_PEOPLE_DATA",""); //取出key为"KEY_PEOPLE_DATA"的值,如果值为空,则将第二个参数作为默认值赋值if(peopleJson!="") //防空判断{Gson gson = new Gson(); People people = gson.fromJson(peopleJson, People.class); //将json字符串转换成 people对象}4. 从SP中查询javaBean集合SharedPreferences sp = getSharedPreferences("SP_PEOPLE_LIST",Activity.MODE_PRIVATE);//创建sp对象,如果有key为"SP_PEOPLE"的sp就取出peopleListJson = sp.getString("KEY_PEOPLE_LIST_DATA",""); //取出key为"KEY_PEOPLE_DATA"的值,如果值为空,则将第二个参数作为默认值赋值if(peopleJson!="") //防空判断{Gson gson = new Gson(); List peopleList = gson.fromJson(peopleListJson, new TypeToken() {}.getType()); //将json字符串转换成List集合}5. 删除一个JavaBean

直接把sp干掉。

SharedPreferences sp = getSharedPreferences("SP_PEOPLE",Activity.MODE_PRIVATE);//创建sp对象,如果有key为"SP_PEOPLE"的sp就取出peopleJson = sp.getString("KEY_PEOPLE_DATA",""); SharedPreferences.Editor editor = sp.edit() ;editor.clear();editor.commit();6. 删除List中的某个javaBean

1.先取, 2.转换成List, 3.从List中删掉, 4.转换成新List, 5.存入新json串将原先的替换掉。

SharedPreferences sp = getSharedPreferences("SP_PEOPLE_LIST",Activity.MODE_PRIVATE);peopleListJson = sp.getString("KEY_PEOPLE_LIST_DATA",""); if(peopleJson!="") //防空判断{Gson gson = new Gson(); List peopleList = gson.fromJson(peopleListJson, new TypeToken() {}.getType()); //1.2. 取出并转换成ListpeopleList.remove(position) ; //3.移除第position个的javabeanString jsonStr=gson.toJson(peopleList); //4.将删除完的List转换成JsonSharedPreferences.Editor editor = sp.edit() ;editor.putString("KEY_PEOPLE_LIST_DATA", jsonStr) ; //存入json串editor.commit() ; //提交}7. 更新

先取,将要改变的bean更新了 ,转换成List,存入新json串将原先的替换掉。

SharedPreferences sp = getSharedPreferences("SP_PEOPLE_LIST",Activity.MODE_PRIVATE);peopleListJson = sp.getString("KEY_PEOPLE_LIST_DATA",""); if(peopleJson!="") //防空判断{Gson gson = new Gson(); List peopleList = gson.fromJson(peopleListJson, new TypeToken() {}.getType()); //取出**// 省略的操作:取出,更新bean的操作,添加到List,将新List转换成json**SharedPreferences.Editor editor = sp.edit() ;editor.putString("KEY_PEOPLE_LIST_DATA", jsonStr) ; //存入json串editor.commit() ; //提交}

版权声明 author :shaoduo 原文来自:http://blog.csdn.net/shaoduo 其他出处均为转载,原创作品,欢迎读者批评指正。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3