gpt4 book ai didi

android - 如何从房间 dao 访问中获取光标对象列表?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:31 26 4
gpt4 key购买 nike

我正在尝试更改我的代码以使用房间数据库 API。对于文档表,我定义了实体类 Document,当我查询 getAll() 时,它会返回所有文档。

现在我有 Adapter 的旧实现,它使 Cursor 成为用户(它是一个 CursorAdapter )。在我的 DocumentDao 类中,我定义了一种获取游标对象列表的方法。我的 Dao 类如下:

@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();

关于android - 如何从房间 dao 访问中获取光标对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45326479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com