使用GreenDao存储list集合数据

来源:互联网 发布:java groovy 编辑:程序博客网 时间:2024/06/05 08:00

【转载请注明出处:From李诗雨---http://blog.csdn.net/cjm2484836553/article/details/78279493】

不诗意的女程序猿不是好厨师~


点击下载Demo源代码:源码。

(不知道为什么现在csdn上传资源后,没有不需要积分就下载的选项了,没有积分的宝宝留下邮箱,我单独发给你哈~)


具体步骤:

1.GreenDao的配置 

在Project的build.gradle文件中

buildscript {repositories {    jcenter()}dependencies {    classpath 'com.android.tools.build:gradle:2.2.2'    //GreenDao3依赖    classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'}}

在module的build.gradle文件中

apply plugin: 'com.android.application'apply plugin: 'org.greenrobot.greendao' // apply plugindependencies {compile 'org.greenrobot:greendao:3.2.2' // add library}

2.创建所需的实体类对象

@Entitypublic  class TopItemBean {/**    * uniquekey : 8bf358dcbb02c0392e25428ec6f80707    * title : 贵阳老太坐车丢十万元,民警调近百监控全城走访三天两夜找回    * date : 2017-10-17 09:45    * category : 头条        * author_name : 贵阳晚报    * url : http://mini.eastday.com/mobile/171017094556300.html    * thumbnail_pic_s : http://03.imgmini.eastday.com/mobile/20171017/20171017094556_e03c77b9924d77e2f4d18b9128e7a574_4_mwpm_03200403.jpg    * thumbnail_pic_s02 : http://03.imgmini.eastday.com/mobile/20171017/20171017094556_e03c77b9924d77e2f4d18b9128e7a574_3_mwpm_03200403.jpg    * thumbnail_pic_s03 : http://03.imgmini.eastday.com/mobile/20171017/20171017094556_e03c77b9924d77e2f4d18b9128e7a574_1_mwpm_03200403.jpg */private String uniquekey;private String title;private String date;private String category;private String author_name;private String url;private String thumbnail_pic_s;private String thumbnail_pic_s02;private String thumbnail_pic_s03;//...}

重新build一下工程,会自动生成以下代码:
实体类bean的构造方法和get、set方法
DaoTopItemBean、DaoTopItemBean、DAOS类

3.创建数据库

public class MyGDApplication extends Application {private DaoSession daoSession;@Overridepublic void onCreate() {    super.onCreate();    //设置数据库    setMyDataBase();}private void setMyDataBase() {    //创建数据库topnews.db"    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "topnews.db", null);    //获取可写数据库    SQLiteDatabase db = helper.getWritableDatabase();    //获取数据库对象    DaoMaster daoMaster = new DaoMaster(db);    //获取Dao对象管理者    daoSession = daoMaster.newSession();}public DaoSession getDaoSession() {    return daoSession;}}

相关说明:
DevOpenHelper:创建SQLite数据库的SQLiteOpenHelper的具体实现
DaoMaster:GreenDao的顶级对象,作为数据库对象、用于创建表和删除表
DaoSession:管理所有的Dao对象,Dao对象中存在着增删改查等API

4.获取DaoSession并进行相关的操作
已经创建好了DaoSession和Bean对象,编译后会自动生成我们的TopItemBeanDao对象,可通过DaoSession获得:

 // get the note DAO    DaoSession daoSession = ((MyGDApplication) getApplication()).getDaoSession();    topItemBeanDao = daoSession.getTopItemBeanDao();

联网获取数据成功后, 采用挨个遍历的方式将集合数据存储到数据库中。

//联网成功的数据保存到数据库中    //遍历数组保存数据    topItemBeanDao.deleteAll();    if(topList!=null&&topList.size()>0) {        for (int i=0;i<topList.size();i++){            TopItemBean topItemBean = topList.get(i);            topItemBeanDao.insert(topItemBean);        }    }

当没有网络时,去数据库查询所有数据,并显示

    //联网获取数据失败,则去数据库中取集合数据    // query all notes, sorted a-z by their text    notesQuery = topItemBeanDao.queryBuilder().build();    List<TopItemBean> dbTopList = notesQuery.list();    //重新装配数据并刷新    topListContainer.clear();    topListContainer.addAll(dbTopList);    litePalRvAdapter.updateTopData(topListContainer);

检测:关闭手机网络,再次进入应用,发现页面会涛声依旧的显示出来,则保存到数据库成功。

?如果含自定义的类怎么办呢?

从网上看到一个很好的处理办法: 使用json!json作为客户端和服务端之间数据传递的载体,不仅能满足我们现在的业务需求,而且我们还有gson这个解析框架来帮我们做转换!简直不要更简单。 举个例子:

public class OtherBean {@Id(autoincrement = true)private Long id;@Propertyprivate Long otherBeanId;@Propertyprivate Long zoneId;@Property@Convert(converter = UserConverter.class, columnType = String.class)private User user;public static class UserConverter implements PropertyConverter<User, String> {    @Override    public User convertToEntityProperty(String databaseValue) {        if (databaseValue == null) {            return null;        }        return new Gson().fromJson(databaseValue, User.class);    }    @Override    public String convertToDatabaseValue(User entityProperty) {        if (entityProperty == null) {            return null;        }        return new Gson().toJson(entityProperty);    }}}


积累点滴,做好自己!

原创粉丝点击