3

ドキュメントには、次のように記載されています。

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

しかし、Eclipse(アンドロイド)では、整数をパラメーターとして渡すオプションしか表示されませんか? 助けはありますか?

4

2 に答える 2

10

オブジェクトはジェネリックを使用して、idのDaoタイプがエンティティに関連付けられていることを強制します。整数を渡すオプションしか表示されない場合dao.queryForId(...)は、誤って次のように dao を定義した可能性があります。

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

最初のジェネリック パラメーターはエンティティの型を指定し、2 番目のジェネリック パラメーターはそのエンティティの 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 が文字列型であるかを定義する必要があります。

@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 に答える