将List数据存到数据库里
List<WifiPasswordBean.ConnectWifiBean> bufferWifiList = new ArrayList<>();
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
SharedPreferences.Editor mEditor = mContext.getSharedPreferences("sqlda",MODE_PRIVATE).edit();
mEditor.putString("wifiLista",gson.toJson(bufferWifiList));
mEditor.apply();
从数据库里取出String数据并转为List
String wifiStr = mContext.getSharedPreferences("sqlda",MODE_PRIVATE).getString("wifiLista","");
if (!"".equals(wifiStr)){
try {
Gson gson = new Gson();
Type type = new TypeToken<List<WifiPasswordBean.ConnectWifiBean>>(){}.getType();
wifiList = gson.fromJson(wifiStr,type);
}catch (Exception e){
Logger.i("解析wifi数据错误");
e.printStackTrace();
}
}
Map同理,例子如下:
Map<String,Integer> maps = new HashMap<>();
maps.put("4532132154",1);
maps.put("5546545623123",5);
maps.put("6465454564",6);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
SharedPreferences.Editor mEditor = mContext.getSharedPreferences("sqlda",MODE_PRIVATE).edit();
mEditor.putString("wifiLista",gson.toJson(maps));
mEditor.apply();
Map<String,Integer> mapkk = new HashMap<>();
String w = mContext.getSharedPreferences("sqlda",MODE_PRIVATE).getString("wifiLista","");
if (!"".equals(w)){
try {
Type type = new TypeToken<Map<String,Integer>>(){}.getType();
mapkk = gson.fromJson(w,type);
}catch (Exception e){
Logger.i("wangrui 解析wifi数据错误");
e.printStackTrace();
}
}
for (String key : mapkk.keySet()) {
Logger.i("wangrui key = " + key + ", value = " + mapkk.get(key));
}