GreenDao

package com.example.dw.dw20181121moni.model;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

@Entity
public class SaveHomeBean {
    @Id(autoincrement = true)
    private Long id;
    private String data;
    @Generated(hash = 666561533)
    public SaveHomeBean(Long id, String data) {
        this.id = id;
        this.data = data;
    }
    @Generated(hash = 1564848766)
    public SaveHomeBean() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getData() {
        return this.data;
    }
    public void setData(String data) {
        this.data = data;
    }
}
package com.example.dw.dw20181121moni.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.dw.dw20181121moni.DaoMaster;
import com.example.dw.dw20181121moni.DaoSession;
import com.example.dw.dw20181121moni.model.GreenBean;
import com.example.dw.dw20181121moni.model.SaveHomeBean;

import java.util.List;

public class DWSqutils {
    private static DWSqutils ourInstance =null;
    private DaoMaster daoMaster;
    private  DaoSession daoSession;
    public static DWSqutils getInstance() {
        if (ourInstance == null){
            ourInstance = new DWSqutils();
        }
        return ourInstance;
    }
    private DWSqutils() {

    }
    public void init(Context context){
        DaoMaster.DevOpenHelper dw = new DaoMaster.DevOpenHelper(context, "dw");
        SQLiteDatabase db = dw.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }
    public void insert(GreenBean greenBean){
        daoSession.getGreenBeanDao().insert(greenBean);
    }
    public void insertHome(SaveHomeBean saveHomeBean){
        daoSession.getSaveHomeBeanDao().insert(saveHomeBean);
    }
    public List<GreenBean> query(){
        List<GreenBean> list = daoSession.getGreenBeanDao().loadAll();
        return list;
    }
    public List<SaveHomeBean> queryHome(){
        List<SaveHomeBean> list = daoSession.getSaveHomeBeanDao().loadAll();
        return list;
    }
}

 

boolean networkAvalible = isNetworkAvalible(context);
        if (networkAvalible){
            doHttpHome();
        }else{
            List<SaveHomeBean> saveHomeBeans = DWSqutils.getInstance().queryHome();
            String s = saveHomeBeans.get(0).getData();
            Gson gson = new Gson();
            HomeBean homeBean = gson.fromJson(s, HomeBean.class);
            tuijianlist = homeBean.getData().getTuijian().getList();
            goodsAdapter.setList(tuijianlist);
        }
 private void doHttpHome() {
        new OkHttp().get(urltwo).setNetCallBack(new OkHttp.NetCallBack() {
            @Override
            public void onSuccess(String result) {
                SaveHomeBean saveHomeBean = new SaveHomeBean();
                saveHomeBean.setData(result);
                DWSqutils.getInstance().insertHome(saveHomeBean);
                Gson gson = new Gson();
                HomeBean homeBean = gson.fromJson(result, HomeBean.class);
                tuijianlist = homeBean.getData().getTuijian().getList();
                goodsAdapter.setList(tuijianlist);
            }
        });
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值