如何解决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>
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。