代码之家  ›  专栏  ›  技术社区  ›  Burhanuddin Rashid Abdilahi

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

  •  3
  • Burhanuddin Rashid Abdilahi  · 技术社区  · 7 年前

    我一直在尝试用谷歌在Android架构组件中提供的空间来实现分页库。但在我的 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();
    
    }
    

    这里是 用户模型。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

    任何帮助都将不胜感激

    3 回复  |  直到 7 年前
        1
  •  3
  •   Burhanuddin Rashid Abdilahi    7 年前

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

        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
  •  3
  •   Ahmed Garhy    4 年前

    使用版本2.3.0-alpha01

    根据房间发布说明

    分页3.0支持:Room现在将支持生成实现for@Query返回类型为androidx的带注释方法。寻呼。PagingSource。

    @Dao interface UserDao {
    @Query("SELECT * FROM users ORDER BY id ASC")
       fun pagingSource(): PagingSource<Int, User>
    }
    
        3
  •  -6
  •   Kevin    6 年前

    尝试使用以下代码:

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

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