天天看點

使用GreenDao存儲list集合資料

【轉載請注明出處: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 plugin

dependencies {
compile 'org.greenrobot:greendao:3.2.2' // add library
}
           

2.建立所需的實體類對象

@Entity
public  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;

@Override
public 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;

@Property
private Long otherBeanId;

@Property
private 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);
    }
}
}
           
積累點滴,做好自己!