SharedPreferences没有保存数组的方法,但是有时候为了保存一个数组而进行序列化,或者动用sqlite都是有点杀猪焉用牛刀的感觉,所以就自己动手改进一下吧。
解决方案:
采用的方式是先转换成JSON,然后保存字符串,取出的时候再讲JSON转换成数组就好了。
public void saveInfo(Context context, String key,
List<Map<String, String>> datas) {
JSONArray mJsonArray = new JSONArray();
for (int i = 0; i < datas.size(); i++) {
Map<String, String> itemMap = datas.get(i);
Iterator<Map.Entry<String, String>> iterator = itemMap.entrySet()
.iterator();
JSONObject object = new JSONObject();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
try {
System.out.println(entry.getKey() + ":" + entry.getValue());
object.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
}
}
mJsonArray.put(object);
}
SharedPreferences sp = context.getSharedPreferences("ubiListName",
Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString(key, mJsonArray.toString());
editor.commit();
}
public List<Map<String, String>> getInfo(Context context, String key) {
List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
SharedPreferences sp = context.getSharedPreferences("ubiListName",
Context.MODE_PRIVATE);
String result = sp.getString(key, "");
try {
JSONArray array = new JSONArray(result);
for (int i = 0; i < array.length(); i++) {
JSONObject itemObject = array.getJSONObject(i);
Map<String, String> itemMap = new HashMap<String, String>();
JSONArray names = itemObject.names();
if (names != null) {
for (int j = 0; j < names.length(); j++) {
String name = names.getString(j);
String value = itemObject.getString(name);
itemMap.put(name, value);
}
}
datas.add(itemMap);
}
} catch (JSONException e) {
}
return datas;
}
============================================
其他方法 就是将 要存储的 数据 进行 toJson 然后存储字符串 在将字符串取出来进行Gson解析成bean对象
Android本地最简单的数据存储,没有之一(让SharedPreferences存取JavaBean对象或List<Bean>)
3.实现
1. 存储单个JavaBean
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集合
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
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集合
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干掉。
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<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串将原先的替换掉。
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() ; //提交
}
●
安卓开发交流群 : 595856941