问题描述
我的视图模型中有以下内容:
// hold the list of comments of a Post
private val _commentsOfPost = MutableLiveData<PagedList<Comment>>()
val commentsOfPost : LiveData<PagedList<Comment>> = _commentsOfPost
fun getCommentsOfPost(postId: Long){
_commentsOfPost.value = commentRepository.getCommentsOfPost(postId) // <--- TYPE MISMATCH
}
因此,发生的事情是,每当Fragment调用getCommentsOfPost()
时,它就会检索属于其ID指定的PagedList
的{{1}}实例中的Comment
。
但是Android告诉我有关Post
的信息(请参见上面的代码片段中的箭头):
Type mismatch
为了完整起见,这是Required: PagedList<Comment>?
Found: LiveData<PagedList<Comment>>
界面:
getCommentsOfPost()
如何更改它以使此错误消失?
解决方法
您应该返回PagedList, 样本:
fun getCommentsOfPost(postId: Long) : PagedList<Comment> {
// your code
// return PagedList<Comment>
}