• 使用sharepreferce记录数组数据


    使用sharepreferce记录数组数据

     1  /**
     2         * 
     3         * sharepreference纪录news数据
     4         * 
     5         * 
     6         */
     7        private static final String   name="newsdata";
     8      
     9        
    10      //保存
    11        public void setDataList(String tag,List<News> datalist) {
    12            SharedPreferences sp=mactivity.getSharedPreferences(name,Context.MODE_PRIVATE);
    13                        if (null == datalist|| datalist.size() <= 0)
    14                                return;
    15 
    16                        Gson gson = new Gson();
    17                        String strJson = gson.toJson(datalist);
    18                        sp.edit().putString(tag,strJson).commit();   
    19                }
    20        
    21      
    22        //获取
    23       public List<News> getDataList(String tag) {
    24           SharedPreferences sp=mactivity.getSharedPreferences(name,Context.MODE_PRIVATE);
    25                        String strJson = sp.getString(tag, null);
    26                        if (null == strJson) {
    27                                return null;
    28                        }
    29                        Gson gson = new Gson();
    30                        List<News> datalist= gson.fromJson(strJson,
    31                                        new TypeToken<List<News>>() {
    32                                        }.getType());
    33                        return datalist;
    34                }
  • 相关阅读:
    onclick中的函数的参数this
    classList的使用
    设置点击鼠标时不跳转
    模块补充shutil,logging
    re模块拾遗和递归函数
    正则表达式-re模块
    软件开发规范
    自定义模块2
    常用模块
    初识自定义模块
  • 原文地址:https://www.cnblogs.com/wangying222/p/5587786.html
Copyright © 2020-2023  润新知