ドキュメントには、次のように記載されています。
Account account = accountDao.queryForId("John Smith");
if (account == null) {
// the name "John Smith" does not match any rows
}
しかし、Eclipse(アンドロイド)では、整数をパラメーターとして渡すオプションしか表示されませんか? 助けはありますか?
オブジェクトはジェネリックを使用して、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
次に、String
IDでクエリを実行できます。
Account account = accountDao.queryForId("John Smith");
最初に、どのエンティティ 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
}