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