android greendao的外部封裝不太友好。

來源:互聯網
上載者:User

標籤:

https://github.com/greenrobot/greenDAO

下載下官網的樣本,有完整的封裝版本,但自已封裝是碰到很多問題。

因greenDao的Master和Session中很多方法完全是隱藏的,只屬於此類。

很多資訊也是隱藏的。

 

只能基於繼承此兩類來實現(像官網樣本一樣)

 

想通過公有方法來實現最基本的一個demo也碰到很多坑。

 

這是樣本

     Master.DevOpenHelper dbhelp=new Master.DevOpenHelper(this,"dbname",null);        SQLiteDatabase db=dbhelp.getWritableDatabase();        DaoConfig dc=new DaoConfig(db,BaiduUserDao.class);        BaiduUserDao dao=new BaiduUserDao(dc);        BaiduUser baiduUser=new BaiduUser(null,"tel","name1","pwd","mail");        dao.insert(baiduUser);

有兩個坑 

一:

BaiduUserDao類必須設定 TABLENAME靜態屬性
public static final String TABLENAME = "BaiduUser";
不然會報錯
java.lang.NoSuchFieldException: TABLENAME

二必須設定Properties靜態內部類
public static class Properties {
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
public final static Property Tel = new Property(1, String.class, "telphone", false, "Tel");
public final static Property UserName = new Property(2, String.class, "username", false, "UserName");
public final static Property Email = new Property(3, String.class, "password", false, "Email");
public final static Property PassWord = new Property(4, String.class, "email", false, "PassWord");
};
不然會報錯
java.lang.ClassNotFoundException: com.mac.cdp.androidbaidusign.dao.BaiduUserDao$Properties

好在有異常資訊提示,不然類內部沒有限制,也沒有明顯的說明。
但也可以理解,限制到構造裡,太浪費了,而靜態方法更省欄位。

……

我還是照著git上樣本的測試代碼,通過繼承session和master來吧。

我也真是沒事找事

想不按官網的路子封裝幾個工具類,發覺無路可走,只能按他的路子來

session 的registerDao 方法
master 的registerDaoClass 方法
都只能在子類中調用。不提供public方法,只能在子類實現中調用,或套一層public個方法。


android greendao的外部封裝不太友好。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.