我一直尝试使用谷歌在Android架构组件中提供的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());
}
}
我参考了以下示例:
我提出了问题HERE
任何帮助将不胜感激
答案 0 :(得分:3)
使用2.3.0-alpha01版
根据房间发布说明
Paging 3.0支持:Room现在将支持为返回类型为androidx.paging.PagingSource的@Query带注释的方法生成实现。
@Dao interface UserDao {
@Query("SELECT * FROM users ORDER BY id ASC")
fun pagingSource(): PagingSource<Int, User>
}
答案 1 :(得分:2)
我通过将库更新到最新版本
来解决了这个问题 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'
答案 2 :(得分:-1)
尝试使用以下代码:
@Query("select * from tbbook")
List<BookEntity> getBooks();
请勿尝试更改返回类型。例如:ArrayList<BookEntity> getBooks();