简体   繁体   中英

Android - Type mismatch with LiveData variable

I have the following in my viewmodel :

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

So, what happens is that whenever the getCommentsOfPost() is called by the Fragment, it retrieves a PagedList of Comment instances belonging to a Post specified by its ID.

But Android tells me about a Type mismatch ( see the arrow in the codesnippet above):

Required: PagedList<Comment>?
Found: LiveData<PagedList<Comment>>

For the sake of completeness, this is the getCommentsOfPost() interface:

fun getCommentsOfPost(postId: Long) : LiveData<PagedList<Comment>>

How can I change it so that this error disappears ?

You should return to PagedList, sample:

fun getCommentsOfPost(postId: Long) : PagedList<Comment> {
   // your code
   // return PagedList<Comment>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM