ACache是一个为android制定的 轻量级的 开源缓存框架。 我们可以通过ACache缓存机制来实现对数据的缓存处理,下面通过一个实例来认识ACache缓存。
思路:我们在activity_main.xml中创建ListView布局,每一个item中放置初始数据,然后在MainActivity中对每一个item添加点击事件,在点击事件中将每一个item中的数据加入到ACache缓存中。最后再添加一个按钮,通过Intent跳转到SecondActivity中,而我们在SecondActivity界面中,就把缓存中的数据再提取出来,组成一个新的ListView.
首先我们需要自定义一个缓存类,我这里取名为item_acache,并且添加了三种方法,代码如下:
package com.example.test;
import android.content.Context;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by lenovo on 2017/10/30.
*/
public class item_acache {
private static ArrayList<Map<String,Object>> data=new ArrayList<Map<String, Object>>();
//data是一个二维数组,数组中的每一个值都是一个Map类型的数据
public static void set_goods_Cache(Context context, Map<String,Object> list){
ACache aCache = ACache.get(context);
data.add(list);
aCache.put("goods_cache",data);
//把数据加入到缓存中,设置键为"goods_cache"
}
public static ArrayList<Map<String,Object>> get_goods_Cache(Context context){
ACache aCache = ACache.get(context);
ArrayList<Map<String,Object>> cache_goods = (ArrayList<Map<String,Object>>)aCache.getAsObject("goods_cache");
//aCache.getAsObject()通过该方法得到缓存中的数据,将类型