【发布时间】:2017-07-26 12:05:01
【问题描述】:
我正在尝试更改我的代码以使用房间数据库 API。对于文档表,我定义了实体类Document,当我查询getAll() 时,它会返回所有文档。
现在我有旧的 Adapter 实现,它使 Cursor 的用户(它是 CursorAdapter )。在我的DocumentDao 类中,我定义了一种获取游标对象列表的方法。我的道类如下:
@Dao
public interface DocumentDao {
@Query("SELECT * FROM documents")
List<com.myapp.room.entity.Document> getAll();
@Query("SELECT * FROM documents")
List<Cursor> getCursorAll();
}
在编译期间我收到以下错误:
Error:(20, 18) error: Not sure how to convert a Cursor to this method's return type
Room 的官方指南指出:
如果您的应用逻辑需要直接访问返回行,您可以 从您的查询中返回一个 Cursor 对象,如下所示 代码sn-p:
@Dao
public interface MyDao {
@Query("SELECT * FROM user WHERE age > :minAge LIMIT 5")
public Cursor loadRawUsersOlderThan(int minAge);
}
我的问题是我需要为此目的编写转换器吗?
【问题讨论】:
标签: android android-sqlite android-room