如何从房间 dao 访问中获取光标对象列表?
How to get list of cursor object from room dao access?
我正在尝试更改我的代码以使用房间数据库 API。对于文档 table 我定义了实体 class Document
,当我查询 getAll()
它 returns 我所有的文档。
现在我有 Adapter 的旧实现,它使 Cursor
成为用户(它是 CursorAdapter
)。在我的 DocumentDao
class 中,我定义了一种获取游标对象列表的方法。我的道 class 如下:
@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 的官方指南指出:
If your app's logic requires direct access to the return rows, you can
return a Cursor object from your queries, as shown in the following
code snippet:
@Dao
public interface MyDao {
@Query("SELECT * FROM user WHERE age > :minAge LIMIT 5")
public Cursor loadRawUsersOlderThan(int minAge);
}
我的问题是我需要为此编写转换器吗?
您返回的是 List<Cursor>
而不是 Cursor
。变化:
@Query("SELECT * FROM documents")
List<Cursor> getCursorAll();
为了
@Query("SELECT * FROM documents")
Cursor getCursorAll();
我正在尝试更改我的代码以使用房间数据库 API。对于文档 table 我定义了实体 class Document
,当我查询 getAll()
它 returns 我所有的文档。
现在我有 Adapter 的旧实现,它使 Cursor
成为用户(它是 CursorAdapter
)。在我的 DocumentDao
class 中,我定义了一种获取游标对象列表的方法。我的道 class 如下:
@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 的官方指南指出:
If your app's logic requires direct access to the return rows, you can return a Cursor object from your queries, as shown in the following code snippet:
@Dao
public interface MyDao {
@Query("SELECT * FROM user WHERE age > :minAge LIMIT 5")
public Cursor loadRawUsersOlderThan(int minAge);
}
我的问题是我需要为此编写转换器吗?
您返回的是 List<Cursor>
而不是 Cursor
。变化:
@Query("SELECT * FROM documents")
List<Cursor> getCursorAll();
为了
@Query("SELECT * FROM documents")
Cursor getCursorAll();