我试图模仿Google的codelab来使用新的Paging 3库,当我尝试让一个Room DAO方法返回PagingSource
时,遇到了以下错误:
D:\Programming\Android\something\app\build\tmp\kapt3\stubs\debug\com\someapp\something\data\db\UsersDao.java:38: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser>).
public abstract androidx.paging.PagingSource<java.lang.Integer, com.someapp.something.data.db.GithubUser> getUserByUserName(@org.jetbrains.annotations.NotNull()
^D:\Programming\Android\something\app\build\tmp\kapt3\stubs\debug\com\someapp\something\data\db\UsersDao.java:43: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser>).
public abstract androidx.paging.PagingSource<java.lang.Integer, com.someapp.something.data.db.GithubUser> getUserByNote(@org.jetbrains.annotations.NotNull()
这是我的
UsersDao.kt
文件:@Dao
interface UsersDao {
@Insert
fun insert(user: GithubUser): Completable
@Insert
fun insert(userList: List<GithubUser>): Completable
@Query("DELETE FROM userDb")
fun clearDb(): Completable
@Query("SELECT * FROM userDb")
fun getAllUsers(): Single<List<GithubUser>>
@Query("SELECT EXISTS(SELECT 1 FROM userDb WHERE username LIKE :userName)")
fun checkIfUserExists(userName: String): Boolean
@Query("SELECT note FROM userDb WHERE username LIKE :userName")
fun getNoteByUserName(userName: String): Single<String>
@Query("SELECT * FROM userDb WHERE username LIKE :userName")
fun getUserByUserName(userName: String): PagingSource<Int, GithubUser>
@Query("SELECT * FROM userDb WHERE note LIKE :note")
fun getUserByNote(note: String): PagingSource<Int, GithubUser>
}
我的 GithubUser.kt
文件看起来像这样:
@Entity(tableName = "userDb", indices = arrayOf(Index(value = ["username"], unique = true)))
class GithubUser (
var username: String,
var note: String,
var url: String,
var avatarUrl: String
) {
@PrimaryKey(autoGenerate = true)
var uid = 0
}
在分页Codelab的code中,DAO方法只返回一个没有额外注释/Gradle或其他魔法选项的
PagingSource
。我还查看了来自Github的其他示例,如this和this,它们使用Paging 3库,完全没有问题地返回PagingSource
。有人能告诉我是否遗漏了什么吗?注意:在错误本身之前,我总是收到关于ANTLR工具版本4.5.3用于代码生成与当前运行时版本4.7.1不匹配的警告,但这个警告本身过去并没有引起任何问题,但我在这里记录一下,以防万一。
编辑:我使用以下Room/Paging lib版本:
implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
implementation 'androidx.room:room-rxjava2:2.2.5'
implementation "androidx.paging:paging-runtime:3.0.0-alpha03"
implementation 'androidx.paging:paging-rxjava2:3.0.0-alpha03'