2016-03-31 48 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文件并更改相应类中的导入已解决了我的问题!谢谢! –