Android使用SharedPreferences保存List列表数据
发布日期:2021-07-01 00:00:03 浏览次数:2 分类:技术文章

本文共 854 字,大约阅读时间需要 2 分钟。

前言

使用此功能是用于保存用户的搜索记录标签云

思路

在用户点击了搜索的同时 把标签显示出来 同时保存到本地

以便于下次进入的时候直接读缓存的搜索记录显示

使用该方法需要集成Gson工具

保存List集合

private static final String KEY_SEARCH_MSG = "key_search_msg";  SharedPreferences sp = this.getSharedPreferences("str_list", Activity.MODE_PRIVATE);        Gson gson = new Gson();        String str = gson.toJson(historyList);        SharedPreferences.Editor editor = sp.edit();        editor.putString(KEY_SEARCH_MSG, str);        editor.commit();

解析获取List集合

使用该方法需要继承Gson工具

SharedPreferences sp = getSharedPreferences("str_list", Activity.MODE_PRIVATE);        String listJson = sp.getString(KEY_SEARCH_MSG, "");        if (!listJson.equals("")) {            Gson gson = new Gson();            historyList = gson.fromJson(listJson, new TypeToken
>() { }.getType()); } mLabelsHistory.setLabels(historyList);//保存标签云

转载地址:https://lvshichunqiu.blog.csdn.net/article/details/107529661 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:使用Git将代码保存到码云(gitee)远程服务器
下一篇:android 高德地图按照经纬度导航出现路线偏差 原因:坐标系不一致

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月08日 10时30分53秒