分页编译问题:不确定如何将游标转换为此方法的返回类型。

3

我一直在尝试在Android Architecture Component中使用Google提供的Room和Paging Library。但是在我的UserDao类中编译时出现错误。

这里是错误信息:

Error:(22, 42) error: Not sure how to convert a Cursor to this method's return type

我的问题是返回类型是什么?

UserDao.java

@Dao
public interface UserDao {
    @Query("SELECT * FROM user")
    LiveData<List<User>> getAll();

    //Compile Error is here : Not sure how to convert a Cursor to this method's return type
    @Query("SELECT * FROM user")
    LivePagedListProvider<Integer, User> userByPagination();

}

这里是UserModel.java
public class UserModel extends AndroidViewModel {

    private final UserDao userDao;

    public UserModel(Application application) {
        super(application);
        userDao = RoomDB.getDefaultInstance().userDao();
    }

    public LiveData<List<User>> getAllUser() {
        return userDao.getAll();
    }


    public LiveData<PagedList<User>> getAllUserPagination() {
        return userDao.userByPagination().create(
                /* initial load position */ 0,
                new PagedList.Config.Builder()
                        .setEnablePlaceholders(true)
                        .setPageSize(10)
                        .setPrefetchDistance(5)
                        .build());
    }
}

我参考了以下示例:

示例1

Google文档

我在这里提出了问题。

如果有帮助,将不胜感激。

3个回答

3

我通过更新库到最新版本解决了这个问题。

    compile 'android.arch.persistence.room:runtime:1.0.0-beta2'
    annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-beta2'
    compile 'android.arch.paging:runtime:1.0.0-alpha3'

    compile 'android.arch.lifecycle:runtime:1.0.0-beta2'
    compile 'android.arch.lifecycle:extensions:1.0.0-beta2'
    annotationProcessor 'android.arch.lifecycle:compiler:1.0.0-beta2'

3

请使用版本2.3.0-alpha01

根据Room发布说明

Paging 3.0支持:Room现在支持为@Query注释方法生成实现,其返回类型为androidx.paging.PagingSource。

@Dao interface UserDao {
@Query("SELECT * FROM users ORDER BY id ASC")
   fun pagingSource(): PagingSource<Int, User>
}

-6

尝试使用以下代码:

@Query("select * from tbbook")
List<BookEntity> getBooks();

不要试图更改返回类型。例如:ArrayList<BookEntity> getBooks();


问题是关于如何在Room库的Dao中返回PagingSource(来自Paging库)。 - Mitch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接