数据库+3级缓存(ok请求)

主页面(3级缓存)

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @BindView(R.id.lv_data)
    ListView lvData;
    private List<Category> datas;

    private DataAdapter adapter;

    private String url = "https://www.zhaoapi.cn/product/getCatagory";
    private CategoryDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        dao = DBUtils.getInstance(this).getDao();

        // 首先从内存中有没有数据
        datas = BaseApplication.getInstance().getData();
        adapter = new DataAdapter(this, datas);
        lvData.setAdapter(adapter);

        if (datas == null || datas.size() == 0) {
            Log.i(TAG, "内存中无数据");
            // 从数据库中取
            List<Category> list = getDataFromDB();
            // 数据库中也是空的
            if (list == null || list.size() == 0) {
                Log.i(TAG, "数据库中为空");
                // 从网络中取数据
                getDataFromNet();
            } else {
                Log.i(TAG, "数据库中不为空");
                // 数据库中有数据的时候
                putDataToMemory(list);
            }
        }
    }

    /**
     * 从网络中取数据
     */
    private void getDataFromNet() {
        Log.i(TAG, "从网络中取");
        HttpUtils.getInstance().get(url, new CallBack() {
            @Override
            public void onSuccess(Object o) {
                MessageBean bean = (MessageBean) o;
                if (bean != null) {
                    List<Category> data = bean.getData();
                    if (data != null) {
                        Log.i(TAG, "onSuccess: 网络不为空");
                        // 将网络请求的数据存到内存和数据库中
                        putDataToMemory(data);
                        // 蒋数据添加到数据库中
                        for (Category category : data) {
                            dao.insertOrReplace(category);
                        }

                    }
                }
            }

            @Override
            public void onFailed(Exception e) {

            }
        }, MessageBean.class);
    }

    /**
     * 蒋数据添加到内存中
     *
     * @param data
     */
    private void putDataToMemory(List<Category> data) {
        Log.i(TAG, "向内存中存数据");
        datas.clear();
        // 将数据添加到内存中
        datas.addAll(data);
        adapter.notifyDataSetChanged();
        BaseApplication.getInstance().setData(datas);
    }

    /**
     * 从数据库中取数据
     *
     * @return
     */
    private List<Category> getDataFromDB() {
        Log.i(TAG, "从数据库中取");
        // select * from category where 1=1
//        List<Category> categories = dao.queryRaw("where _id=?", "1");
        // 查询所有数据
        List<Category> categories = dao.loadAll();
        if (categories == null || categories.size() == 0) {
            Log.i(TAG, "getDataFromDB: null");
        } else {
            Log.i(TAG, "getDataFromDB: " + categories.size());
        }
        return categories;
    }
}
 
数据库封装

public class DBUtils {
    private static volatile DBUtils instance;
    private final CategoryDao dao;

    private DBUtils(Context context) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "category.db", null);
        SQLiteDatabase database = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(database);
        DaoSession daoSession = daoMaster.newSession();
        dao = daoSession.getCategoryDao();
    }

    public static DBUtils getInstance(Context context){
        if (instance == null) {
            synchronized (DBUtils.class) {
                if (null == instance) {
                    instance = new DBUtils(context);
                }
            }
        }

        return instance;
    }

    public CategoryDao getDao(){
        return dao;
    }
}
从内存中找数据(在清单中注册)


public class BaseApplication extends Application {
    private static BaseApplication instance;
    private List<Category> list;
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }


    public static BaseApplication getInstance() {
        return instance;
    }


    public List<Category> getData() {
        if (list == null) {
            list = new ArrayList<>();
        }
        return list;
    }

    public void setData(List<Category> list) {
        this.list = list;
    }


}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值