0

我正在尝试DataSource.Factory上课,但在尝试返回我的PageKeyedDataSource实现时出现错误。

class SubredditPageKeyedDataSource(private val service: LedditService,
                               private val subredditName: String): PageKeyedDataSource<String, Children<DataX>>() {

@SuppressLint("CheckResult")
override fun loadInitial(params: LoadInitialParams<String>, callback: LoadInitialCallback<String, Children<DataX>>) {
    service.getSubredditTopics(subredditName).subscribe { result -> callback.onResult(result.data.children, result.data.before, result.data.after) }
}

@SuppressLint("CheckResult")
override fun loadAfter(params: LoadParams<String>, callback: LoadCallback<String, Children<DataX>>) {
    service.getSubredditTopicsAfter(subredditName, after = params.key).subscribe {result -> callback.onResult(result.data.children, result.data.after)}
}

override fun loadBefore(params: LoadParams<String>, callback: LoadCallback<String, Children<DataX>>) {
    // ignored, since we only ever append to our initial load
}

}

现在这里是我的工厂:

class SubredditDataSourceFactory(private val service: LedditService,
                             private val subredditName: String): DataSource.Factory<String, List<Children<DataX>>>() {


override fun create(): DataSource<String, List<Children<DataX>>> {
    return SubredditPageKeyedDataSource(service, subredditName)
    }
}

在上述create()方法中返回我的 DataSource 时,AndroidStudio 给了我一个 IDE 错误:

Type mismatch. 
Required: DataSource<String, List<Children<DataX>>>
Found: SubredditPageKeyedDataSource

我要疯了吗?PageKeyedDataSource从中延伸出来ContiguousDataSource的是 a DataSource。为什么我得到类型不匹配?我检查了分页库的 2 个不同示例,它们的实现与我的完全一样。除非我错过了什么???

4

1 回答 1

0

类型实际上并不匹配。

您的工厂用于:

PageKeyedDataSource<String, List<Children<DataX>>>

但是您的数据源实现了以下类型:

PageKeyedDataSource<String, Children<DataX>>

请注意,一个在右侧有一个孩子列表,另一个是一个孩子。没有 List<> 的应该是正确的,请尝试从代码中删除该部分。

于 2018-10-04T20:57:40.283 回答