SharedPReferences想必大家在项目中都经常会用到,但是如果需要在本地需要存储比较多的数据,存储一个集合的时,发现Sharedpreferences并不
是那么好使了。
分析

如果需要在本地种存储上图中的数据,显而易见的是一个List中存储多个对象,使用Sharedpreferences有局限性,
使用Sqlite个人感觉比较麻烦。
解决方案
推荐一款轻量级的Android缓存框架:ASimpleCache ,github开源项目(国人写的哦) 地址
拷贝下官方说明:
ASimpleCache 是一个为android制定的 轻量级的 开源缓存框架。轻量到只有一个java文件(由十几个类精简而来)。
以下有个小的demo,希望您能喜欢:
ACache mCache = ACache.get(this);mCache.put("test_key1", "test value");mCache.put("test_key2", "test value", 10);//保存10秒,如果超过10秒去获取这个key,将为nullmCache.put("test_key3", "test value", 2 * ACache.TIME_DAY);//保存两天,如果超过两天去获取这个key,将为null获取数据
ACache mCache = ACache.get(this);String value = mCache.getAsString("test_key1"); 官方中并没有提到怎么来存储List,摸索了一下分享给大家: 代码: 存入本地
ACache aCache = ACache.get(mContext); //只能使用List的子类 ArrayList<CarLocation> arrayList = new ArrayList(); //注意:一定要序列化 CarLocation carLocation = new CarLocation(); carLocation.setTitle("测试"); arrayList.add(carLocation); aCache.put("car", arrayList);
取出
ACache aCache = ACache.get(activity); //使用getAsObject(),直接进行强转 ArrayList<CarLocation> carLocations = (ArrayList<CarLocation>) aCache.getAsObject("car");
使用ACache只需要一个类即可 :点击打开链接
新闻热点
疑难解答