2016-03-31 50 views
1

我想從AndroidAnnotations 3.3.2升級到4.0.0版,並且在升級後讓ORMLite工作有問題。 我升級到4.0.0後,建設項目中,我得到的錯誤信息: 「錯誤:無法找到符號類OrmLiteDao」 這部分代碼標記爲紅色:如何調整AndroidAnnotations 3.x compatible ORMLite代碼爲AndroidAnnotations 4.0.0

@OrmLiteDao(helper = DatabaseHelper.class) 
ContactDao mContactDao; 

我的老班(ES )與AndroidAnnotations 3.3.2完美的作品看起來是這樣的:

public class ContactService { 

    private final String TAG = getClass().getSimpleName(); 

    @RootContext 
    Context ctx; 

    @OrmLiteDao(helper = DatabaseHelper.class) 
    ContactDao mContactDao; 

    public Contact getContact(Integer contactId) throws ItemNotFoundException, SQLException { 
     Contact contact = mContactDao.queryForId(contactId); 
     if (contact == null) { 
      Log.e(TAG, "Contact not found in database"); 
      throw new ItemNotFoundException(); 
     } 

     return contact; 
    } 

    public List<Contact> getContacts() throws ItemNotFoundException, SQLException { 
     List<Contact> contact = mContactDao.queryForAll(); 
     if (contact == null) { 
      Log.e(TAG, "Contacts not found in database"); 
      throw new ItemNotFoundException(); 
     } 

     return contact; 
    } 

    public Contact createContact(Contact contact) throws SQLException { 
     try { 
      mContactDao.create(contact); 
     } catch (SQLException e) { 
      Log.e(TAG, e.getLocalizedMessage()); 
     } 

     Log.d(TAG, "New Contact ID: " + contact.getId()); 

     return contact; 
    } 

} 

而我的ContactDAO類是比較空的,因爲我都在的ContactService類中的方法:

public class ContactDao extends BaseDaoImpl<Contact, Integer> { 

    public ContactDao(Class<Contact> dataClass) throws SQLException { 
     super(dataClass); 
    } 

    public ContactDao(ConnectionSource connectionSource, Class<Contact> dataClass) throws SQLException { 
     super(connectionSource, dataClass); 
    } 

    public ContactDao(ConnectionSource connectionSource, DatabaseTableConfig<Contact> tableConfig) throws SQLException { 
     super(connectionSource, tableConfig); 
    } 

    public ContactDao(ConnectionSource connectionSource) throws SQLException { 
     super(connectionSource, Contact.class); 
    } 
} 

按照AndroidAnnotations維基(https://github.com/excilys/androidannotations/wiki/Ormlite)我必須要改變的代碼如下:

@EActivity 
public class MyActivity extends Activity { 

    @OrmLiteDao(helper = DatabaseHelper.class) 
    void setUserDao(UserDao userDao){ 
     // do something with userDao 
    } 

} 

目前尚不清楚對我,我怎麼也得調整我上面的代碼到新格式(見下文),以使其與AndroidAnnotations 4.0.0一起工作。任何人都可以告訴我如何調整我的代碼,使其與AndroidAnnotations 4.0.0兼容?

謝謝。

+1

由於'@ OrmLiteDao'是ormlite插件的一部分(如鏈接的wiki頁面所述),您是否將該插件添加爲依賴項?也就是說,你是否已經將'compile'org.androidannotations:ormlite:4.0.0''添加到'build.gradle'中?在版本3.3.2中,註釋仍然是主要API的一部分,但從那時起該項目採取了更加模塊化的方法。 –

回答

1

您不必按照您在上一個示例中編寫的方式更改代碼。在那裏,你正在使用方法注入,但這是一種替代方法,現場注入仍然在AA 4.0.0中起作用。

但是,您必須添加更多的依賴關係,併爲新的程序包名稱調整導入。

的build.gradle

compile "org.androidannotations:androidannotations-api:4.0.0" 
apt "org.androidannotations:androidannotations:4.0.0" 
compile "org.androidannotations:ormlite-api:4.0.0" 
apt "org.androidannotations:ormlite:4.0.0" 

的.java文件在使用@OrmLiteDao

import org.androidannotations.ormlite.annotations.OrmLiteDao; 

你可以閱讀這些說明here

+0

完美!將這4行添加到build.gradle文件並更改相應類中的導入已解決了我的問題!謝謝! –