3

在文档中提到了以下内容:

Account account = accountDao.queryForId("John Smith");
if (account == null) {
    // the name "John Smith" does not match any rows
}   

但在 Eclipse(android) 中,我只看到将整数作为参数传递的选项?有什么帮助吗?

4

2 回答 2

10

对象使用泛型来强制 id的Dao类型与您的实体相关联。如果您只看到将整数传递给的选项,dao.queryForId(...)那么您可能错误地将 dao 定义为:

Dao<Account, Integer> accountDao = getDao(Account.class);

第一个通用参数指定实体的类型,第二个通用参数指定该实体中 ID 字段的类型。有了Integer,你就会调用accountDao.queryForId(Integer)

正如@Tomas 提到的,您需要使用以下内容定义您的 DOA:

Dao<Account, String> accountDao = getDao(Account.class);

然后可以Account通过Stringid 查询:

Account account = accountDao.queryForId("John Smith");
于 2013-03-21T21:19:57.393 回答
3

首先,您应该定义哪些实体 ID 是 String 类型:

@DatabaseTable()
public class Account {

    @DatabaseField(id = true)
    private String mFullName;
    ...
}

然后你应该根据实体类型和它的ID类型来声明Dao对象。现在您可以使用 ID 类型为 String 的 queryForId:

Dao<Account, String> accountDao = getAccountDao();
Account account = accountDao.queryForId("John Smith");
if (account == null) {
    // the name "John Smith" does not match any rows
}
于 2013-03-21T11:43:30.093 回答