如何为AndroidAnnotations 4.0.0调整AndroidAnnotations 3.x兼容的ORMLite代码

时间:2016-03-31 18:31:44

标签: android ormlite android-annotations

我尝试从AndroidAnnotations 3.3.2升级到版本4.0.0,并且在升级后让ORMLite工作时出现问题。 升级到4.0.0并构建项目后,我收到错误消息: "错误:找不到符号类OrmLiteDao" 此代码部分用红色标记:

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

我与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 Wiki(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 个答案:

答案 0 :(得分: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"
使用@OrmLiteDao

.java 文件:

import org.androidannotations.ormlite.annotations.OrmLiteDao;

您可以阅读这些说明here