1. 存储单个JavaBean
1 2 3 4 5 6 7 8 9 10
| SharedPreferences 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); //将对象转换成Json editor = sp.edit() ; editor.putString("KEY_PEOPLE_DATA", jsonStr) ; //存入json串 editor.commit() ; //提交 ShowDialog("您已经保存成功");
|
2. 存储JavBean的List集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| SharedPreferences sp = getSharedPreferences("SP_PEOPLE_List",Activity.MODE_PRIVATE);//创建sp对象 List<People> peopleList = new ArrayList<People>() ; //创建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转换成Json SharedPreferences.Editor editor = sp.edit() ; editor.putString("KEY_PEOPLE_LIST_DATA", jsonStr) ; //存入json串 editor.commit() ; //提交 ShowDialog("您已经保存成功");
|
3. 从SP中查询一个JavaBean
1 2 3 4 5 6 7
| SharedPreferences 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集合
1 2 3 4 5 6 7
| 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<People> peopleList = gson.fromJson(peopleListJson, new TypeToken<List<People>>() {}.getType()); //将json字符串转换成List集合 }
|
5. 删除一个JavaBean
直接把sp干掉。
1 2 3 4 5
| 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串将原先的替换掉。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| SharedPreferences sp = getSharedPreferences("SP_PEOPLE_LIST",Activity.MODE_PRIVATE); peopleListJson = sp.getString("KEY_PEOPLE_LIST_DATA",""); if(peopleJson!="") //防空判断 { Gson gson = new Gson(); List<People> peopleList = gson.fromJson(peopleListJson, new TypeToken<List<People>>() { }.getType()); //1.2\. 取出并转换成List
peopleList.remove(position) ; //3.移除第position个的javabean String jsonStr=gson.toJson(peopleList); //4.将删除完的List转换成Json SharedPreferences.Editor editor = sp.edit() ; editor.putString("KEY_PEOPLE_LIST_DATA", jsonStr) ; //存入json串 editor.commit() ; //提交 }
|
7. 更新
先取,将要改变的bean更新了 ,转换成List,存入新json串将原先的替换掉。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| SharedPreferences sp = getSharedPreferences("SP_PEOPLE_LIST",Activity.MODE_PRIVATE); peopleListJson = sp.getString("KEY_PEOPLE_LIST_DATA",""); if(peopleJson!="") //防空判断 { Gson gson = new Gson(); List<People> peopleList = gson.fromJson(peopleListJson, new TypeToken<List<People>>() { }.getType()); //取出
**// 省略的操作:取出,更新bean的操作,添加到List,将新List转换成json**
SharedPreferences.Editor editor = sp.edit() ; editor.putString("KEY_PEOPLE_LIST_DATA", jsonStr) ; //存入json串 editor.commit() ; //提交 }
|