Android-类型与LiveData变量不匹配

问题描述

我的视图模型中有以下内容:

// 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>
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...