Android体系结构组件分页DataSource.Factory错误

时间:2018-10-04 20:11:22

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

我正在尝试创建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

我疯了吗? PageKeyedDataSourceContiguousDataSource的{​​{1}}扩展而来。为什么会出现类型不匹配?我检查了分页库的2个不同示例,它们的实现与我的完全相同。除非我错过了什么?

1 个答案:

答案 0 :(得分:0)

类型实际上不匹配。

您的工厂用于:

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

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

PageKeyedDataSource<String, Children<DataX>>

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