依赖:
主app中:
buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0' } }自己的项目中:apply plugin:'org.greenrobot.greendao'//加在开头第二行 //加在android的大括号里面greendao{ schemaVersion 1 daoPackage'bwei.com.myapplication.gen' targetGenDir'src/main/java' }compile 'org.greenrobot:greendao:3.1.0' compile 'org.greenrobot:greendao-generator:3.1.0'
实体类创建数据库
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; } }
Bean类的创建:
@Entity public class Category {//@Id与@Entity必写,然后在Build里面Make进行编译生成相应的表结构 @Idprivate Long cid; private String createtime; private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getCreatetime() { return this.createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Long getCid() { return this.cid; } public void setCid(Long cid) { this.cid = cid; } @Generated(hash = 115230139) public Category(Long cid, String createtime, String name) { this.cid = cid; this.createtime = createtime; this.name = name; } @Generated(hash = 1150634039) public Category() { } }@Id private Long cid; private String createtime; private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getCreatetime() { return this.createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public Long getCid() { return this.cid; } public void setCid(Long cid) { this.cid = cid; } @Generated(hash = 115230139) public Category(Long cid, String createtime, String name) { this.cid = cid; this.createtime = createtime; this.name = name; } @Generated(hash = 1150634039) public Category() { }}
创建一个Applocation
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;
}
}
二级缓存
//获取到数据库 dao = DBUtils.getInstance(getActivity()).getDao(); list = getDataFromDB();//查询数据库 viewAdapter = new RecyclerViewAdapter(getActivity(), list); //如果我的数据库没有数据 if (list == null || list.size() == 0) { Toast.makeText(getActivity(), "数据库为空", Toast.LENGTH_SHORT).show(); //从网络中取数据 getDataFromNet(); } rlv.setAdapter(viewAdapter); return view; }
//通过网络获取数据 public void getDataFromNet() { Retrofit build = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .baseUrl("http://gank.io/api/") .build(); Intent intent = build.create(Intent.class); Call<AllBean<List<child_Bean>>> call = intent.call(10, 1); call.enqueue(new Callback<AllBean<List<child_Bean>>>() { @Override public void onResponse(Call<AllBean<List<child_Bean>>> call, Response<AllBean<List<child_Bean>>> response) { AllBean<List<child_Bean>> body = response.body(); List<child_Bean> results = body.getResults(); for (int i = 0; i < results.size(); i++) { Category category = new Category(); category.setCreatetime(results.get(i).getDesc()); category.setName(results.get(i).getWho()); dao.insertOrReplace(category); } viewAdapter.notifyDataSetChanged(); } @Override public void onFailure(Call<AllBean<List<child_Bean>>> call, Throwable t) { } }); }
//查询数据 public List<Category> getDataFromDB() { list = dao.loadAll(); return list; }