【问题标题】:How to get list of cursor object from room dao access?如何从房间 dao 访问中获取光标对象列表?
【发布时间】: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


    【解决方案1】:

    您返回的是List&lt;Cursor&gt;,而不是Cursor。变化:

    @Query("SELECT * FROM documents")
    List<Cursor> getCursorAll();
    

    @Query("SELECT * FROM documents")
    Cursor getCursorAll();
    

    【讨论】:

    • 好吧,我完全忘记了 Cursor 本身有多个对象,我只需要使用 cursor.moveToNext() 对它们进行迭代:) 我想我应该删除我的问题?
    • 就这样吧。任何人都可能犯同样的错误;)
    猜你喜欢
    • 2016-12-04
    • 2015-08-05
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多