【问题标题】:Paging Compile Issue : Not sure how to convert a Cursor to this method's return type分页编译问题:不确定如何将游标转换为此方法的返回类型
【发布时间】:2017-10-10 13:54:29
【问题描述】:

我已尝试在 Android 架构组件中使用 google 提供的 Room 实现分页库。但它在我的 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());
    }
}

我参考了以下示例:

Sample 1

Google Doc

我已提出问题HERE

任何帮助将不胜感激

【问题讨论】:

    标签: java android paging android-room android-architecture-components


    【解决方案1】:

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

        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】:

      使用版本 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>
      }
      

      【讨论】:

        【解决方案3】:

        尝试使用以下代码:

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

        不要尝试更改返回类型。例如:ArrayList&lt;BookEntity&gt; getBooks();

        【讨论】:

        • 问题是关于如何在Room库的Dao中返回PagingSource(来自Paging库)
        猜你喜欢
        • 2020-03-28
        • 2022-01-23
        • 2019-04-23
        • 2021-06-20
        • 2019-12-12
        • 1970-01-01
        • 2018-03-08
        • 1970-01-01
        • 2019-12-03
        相关资源
        最近更新 更多