首页 文章

如何从房间访问中获取游标对象列表?

提问于
浏览
6

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

现在我有适应器的旧实现,它使用户 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的官方指南指出:

如果您的应用程序逻辑需要直接访问返回行,则可以从查询中返回Cursor对象,如以下代码段所示:

@Dao
public interface MyDao {
    @Query("SELECT * FROM user WHERE age > :minAge LIMIT 5")
    public Cursor loadRawUsersOlderThan(int minAge);
}

我的问题是我是否需要为此目的编写转换器?

1 回答

  • 8

    你正在返回 List<Cursor> 而不是 Cursor . 更改:

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

    对于

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

相关问题