GreenDao数据库
greendao 配置
主项目 classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.1’
子项目 apply plugin: ‘org.greenrobot.greendao’
放在android下面
greendao {
schemaVersion 1//数据库版本号
daoPackage ‘com.example.moli2.db’//新建一个db包,把生成的三个包放在里面
targetGenDir ‘src/main/java’//设置DaoMaster、DaoSession、Dao目录
//targetGenDirTest:设置生成单元测试目录
//generateTests:设置自动生成单元测试用例
}
implementation 'org.greenrobot:greendao:3.2.0'
配置完以后 新建一个数据库要添加的数据的bean类
@Entity
public class SQBean {
@Id
Long id;
@Unique
String image;
String name;
}
用小锤子锤一下
在db包里生成DaoMaster、DaoSession、Dao
封装GreenDaoUtils一个工具类
package com.example.pxwzoukao415.utils;
import android.database.sqlite.SQLiteDatabase;
import com.example.pxwzoukao415.app.App;
import com.example.pxwzoukao415.db.DaoMaster;
import com.example.pxwzoukao415.db.DaoSession;
public class GreendaoUtils {
public static GreendaoUtils sgreendao;
private final DaoSession daoSession;
private GreendaoUtils(){
//创建数据库,获取上下文
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(App.context, "users.db");
//读取数据
SQLiteDatabase writableDatabase = devOpenHelper.getWritableDatabase();
//将读取数据存入
DaoMaster daoMaster = new DaoMaster(writableDatabase);
daoSession = daoMaster.newSession();
}
public static GreendaoUtils getSgreendao() {
if (sgreendao==null){
synchronized (GreendaoUtils.class){
if (sgreendao==null){
sgreendao=new GreendaoUtils();
}
}
}
return sgreendao;
}
public DaoSession getDaoSession() {
return daoSession;
}
}
在App里初始化设置
GreendaoUtils.getSgreendao().getDaoSession();
在适配器加入设置的sqBean ,判断
public class ITemAdapter extends XRecyclerView.Adapter<ITemAdapter.IViewHolder> {
private Context context;
private List<ITemBeam.ResultBean> list;
private List<SQBean> sqBeans;
public ITemAdapter(Context context, List<SQBean> sqBeans) {
this.context = context;
this.list = new ArrayList<>();
this.sqBeans = sqBeans;
}
public void setList(List<ITemBeam.ResultBean> list) {
this.list = list;
notifyDataSetChanged();
}
public void addList(List<ITemBeam.ResultBean> list) {
list.addAll(list);
notifyDataSetChanged();
}
@NonNull
@Override
public IViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.layout_cyc2,viewGroup,false);
IViewHolder iViewHolder = new IViewHolder(view);
return iViewHolder;
}
@Override
public void onBindViewHolder(@NonNull IViewHolder iViewHolder, int i) {
if (list!=null&&list.size()>0){
Uri parse = Uri.parse(list.get(i).getLogo());
iViewHolder.item_img.setImageURI(parse);
iViewHolder.item_name.setText(list.get(i).getName());
}else {
Uri parse = Uri.parse(sqBeans.get(i).getImage());
iViewHolder.item_img.setImageURI(parse);
iViewHolder.item_name.setText(sqBeans.get(i).getName());
}
}
@Override
public int getItemCount() {
if (list!=null&&list.size()>0){
return list.size();
}else {
return sqBeans.size();
}
}
public class IViewHolder extends RecyclerView.ViewHolder {
private SimpleDraweeView item_img;
private TextView item_name;
public IViewHolder(@NonNull View itemView) {
super(itemView);
item_img=itemView.findViewById(R.id.item_img);
item_name=itemView.findViewById(R.id.item_name);
}
}
}
在成功的方法里存入数据到数据库,遍历成功的bean里面的数据
for (ITemBeam.ResultBean resultBean : iTemBeam.getResult()) {
GreendaoUtils.getSgreendao().getDaoSession().insertOrReplace(new SQBean(null, resultBean.getLogo(), resultBean.getName()));
}
新建一个判断网络的方法
//判断网络
public boolean isNetword() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isAvailable();
}
当无网时取出的数据
if (!isNetword()) {
List sqBeans = GreendaoUtils.getSgreendao().getDaoSession().loadAll(SQBean.class);
iTemAdapter = new ITemAdapter(this,sqBeans);
xrecycler2.setAdapter(iTemAdapter);
}