使用SharedPreferences保存list
先把对象转成Gson,Gson转成Json串,最后键值对保存
import android.content.Context; import android.content.SharedPreferences; import android.util.Log; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.json.JSONArray; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; /** * Created by B-20 on 2018/1/26. */ public class SharedPreferencesUtil {private static SharedPreferencesUtil util; private static SharedPreferences sp; private SharedPreferencesUtil(Context context, String name) {sp = context.getSharedPreferences(name, Context.MODE_PRIVATE); }/** * 初始化SharedPreferencesUtil,只需要初始化一次 * * @param context 上下文对象 * @param name SharedPreferences Name */ public static SharedPreferencesUtil getInstance(Context context, String name) {if (util == null) {util = new SharedPreferencesUtil(context, name); }return util; }/** * 保存数据到SharedPreferences * * @param key 键 * @param value 需要保存的数据 * @return 保存结果 */ public static boolean putData(String key, Object value) {boolean result; SharedPreferences.Editor editor = sp.edit(); String type = value.getClass().getSimpleName(); try {switch (type) {case "Boolean":editor.putBoolean(key, (Boolean) value); break; case "Long":editor.putLong(key, (Long) value); break; case "Float":editor.putFloat(key, (Float) value); break; case "String":editor.putString(key, (String) value); break; case "Integer":editor.putInt(key, (Integer) value); break; default:Gson gson = new Gson(); String json = gson.toJson(value); editor.putString(key, json); break; }result = true; } catch (Exception e) {result = false; e.printStackTrace(); }editor.apply(); return result; }/** * 获取SharedPreferences中保存的数据 * * @param key 键 * @param defaultValue 获取失败默认值 * @return 从SharedPreferences读取的数据 */ public static Object getData(String key, Object defaultValue) {Object result; String type = defaultValue.getClass().getSimpleName(); try {switch (type) {case "Boolean":result = sp.getBoolean(key, (Boolean) defaultValue); break; case "Long":result = sp.getLong(key, (Long) defaultValue); break; case "Float":result = sp.getFloat(key, (Float) defaultValue); break; case "String":result = sp.getString(key, (String) defaultValue); break; case "Integer":result = sp.getInt(key, (Integer) defaultValue); break; default:Gson gson = new Gson(); String json = sp.getString(key, ""); if (!json.equals("") && json.length() > 0) {result = gson.fromJson(json, defaultValue.getClass()); } else {result = ""; }break; }} catch (Exception e) {result = null; e.printStackTrace(); }return result; }/** * 用于保存集合 * * @param key key * @param list 集合数据 * @return 保存结果 */ public static <T> boolean putListData(String key, List<T> list) {boolean result; // String type = list.get(0).getClass().getSimpleName(); SharedPreferences.Editor editor = sp.edit(); JsonArray array = new JsonArray(); try {Gson gson = new Gson(); for (int i = 0; i < list.size(); i++) {JsonElement obj = gson.toJsonTree(list.get(i)); array.add(obj); } // switch (type) { // // default: // // break; // } editor.putString(key, array.toString()); result = true; } catch (Exception e) {result = false; e.printStackTrace(); }editor.apply(); return result; }/** * 清除指定的数据 */ public static boolean deleteDataByKey(String key){boolean a; SharedPreferences.Editor editor = sp.edit(); try {editor.remove(key); a=true; } catch (Exception e) {a = false; e.printStackTrace(); }editor.apply(); return a; }/** * 获取保存的List * * @param key key * @return 对应的Lis集合 */ public static <T> List<T> getListData(String key, Class<T> cls) {List<T> list = new ArrayList<>(); String json = sp.getString(key, ""); if (!json.equals("") && json.length() > 0) {Gson gson = new Gson(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); for (JsonElement elem : array) {list.add(gson.fromJson(elem, cls)); }}return list; }/** * 用于保存map * * @param key key * @param map map数据 * @return 保存结果 */ public static <K, V> boolean putHashMapData(String key, Map<K, V> map) {boolean result; SharedPreferences.Editor editor = sp.edit(); try {Gson gson = new Gson(); String json = gson.toJson(map); editor.putString(key, json); result = true; } catch (Exception e) {result = false; e.printStackTrace(); }editor.apply(); return result; }/** * * * @param key key * @return HashMap */ public static <V> HashMap<String, V> getHashMapData(String key, Class<V> clsV) {String json = sp.getString(key, ""); HashMap<String, V> map = new HashMap<>(); Gson gson = new Gson(); JsonObject obj = new JsonParser().parse(json).getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet(); for (Map.Entry<String, JsonElement> entry : entrySet) {String entryKey = entry.getKey(); JsonObject value = (JsonObject) entry.getValue(); map.put(entryKey, gson.fromJson(value, clsV)); }Log.e("SharedPreferencesUtil", obj.toString()); return map; } }
使用SharedPreferences保存list相关推荐
- Android SharedPreferences保存和读取对象
SharedPreferences保存和读取对象 1.首先要序列化对象(以下是序列化实体类的样例,不方便贴出实体类全部的代码) public class LoginResult extends Bas ...
- android 使用SharedPreferences保存对象
今天,简单讲讲Android如何使用SharedPreferences保存对象. 记得之前写过一遍博客关于android如何使用SharedPreferences保存List<String> ...
- android SharedPreferences保存list数据
今天,简单讲讲如何使用 SharedPreferences保存list数据. 网上找了很多资料,还是觉得这种比较简单.直接上代码: 保存: public static boolean saveA ...
- Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数
16_采用SharedPreferences保存用户偏好设置参数 Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数 2013-03-08 16_采用Shar ...
- android 同步list数据,android SharedPreferences保存list数据
释放双眼,带上耳机,听听看~! 今天,简单讲讲如何使用SharedPreferences保存list数据. 网上找了很多资料,还是觉得这种比较简单.直接上代码: 保存: public static b ...
- android 清空sp数据,Android 使用 SharedPreferences 保存少量数据的实现代码
1 SharedPreferences 介绍 SharedPreferences是使用键值对的方式来存储数据的 SharedPreferences share = getSharedPreferenc ...
- Android基础入门教程——6.2 数据存储与访问之——SharedPreferences保存用户偏好参数
Android基础入门教程--6.2 数据存储与访问之--SharedPreferences保存用户偏好参数 标签(空格分隔): Android基础入门教程 本节引言: 本节给大家介绍的是第二种存储用 ...
- 实例教程五:采用SharedPreferences保存用户偏好设置参数
android水管工人游戏源码 http://www.eoeandroid.com/thread-207446-1-1.html Android推箱子小游戏 http://www.eoeandroid ...
- SharedPreferences保存对象以及集合,腾讯MMKV使用,保存搜索历史
腾讯MMKV使用 implementation 'com.tencent:mmkv-static:1.2.9' MMKV.initialize(this);//会返回MMVK存储的目录(String) ...
- android 屏幕关闭 eofexception,android EOFException异常解决办法 (SharedPreferences保存对象)...
SharePreferences可以保存String 但是不能保存对象 这不符合面向对象. 为此可以通过序列化和反序列化将Serializable对象和字符串相互转化 所以可以将Serizlizabl ...
最新文章
- 34岁,外企倒闭成功上岸大厂,50K,附面试秘籍
- hbase RowFilter如何根据rowkey查询以及实例实现代码
- 《编码:隐匿在计算机软硬件背后的语言(美)》读书笔记三
- 【强化学习】AC注释版本
- C++ 解析Json
- [ESC] EnTT 学习记录 2
- 爱立信将携手DOCOMO和英特尔构建5G测试环境
- java实验 输入输出_实验十三 Java输入输出(一)
- 关于Keil中,变量函数跨文件引用报错问题
- 面向车路协同的路侧感知仿真系统
- 第5章 运算符、表达式和语句
- 运维体系建设(第二章)
- Havel-Hakimi定理(判断一个度序列是否可图)
- CODING 研发管理系统上线全球加速,助力企业跨区域协作
- matlab中种子填充算法
- Eureka学习笔记
- 伺服电机代替步进电机时应该注意那些细节?
- 【计算机视觉基础】MATLAB程序实现图像中两个像素点的8-邻域、欧几里得距离与出租车距离、互换两像素点的罗森菲尔德8-邻域像素值
- 信号与系统 - 起始状态0- 与初始状态 0+
- 沉寂心念,独看落花碎