3

I'm using PagedListAdapter and pagedListLiveData without Room to display a large list of items.
In code below i'm trying to submit adapter to PagedList.

val pagedListLiveData = LivePagedListBuilder(sourceFactory, pagedListConfig)
       .setFetchExecutor(Executors.newSingleThreadExecutor())
       .build()

val adapter = MyPagedAdapter()

// error below: Type mismatch. Required:Observer<PagedList<MyItem!>!>
pagedListLiveData.observe(lifeCycleOwner, object : Observer<PagedList<MyItem>>() {
        override fun onChanged(items: PagedList<MyItem>) {
            Log.d(LOG_TAG, "submit PagedList")
            adapter.submitList(items)
        }
})

Android Studio highlights an error on the expression:

object : Observer<PagedList<MyItem>>

Error: Type mismatch. Required: Observer<PagedList<MyItem!>!>

How to fix it?

Gregory
  • 575
  • 4
  • 13
  • If you're defining type explicitly then use like this : `Observer?>` this will fix your issue. *(Make sure you've correct import from android package & not from java.util)* – Jeel Vankhede Apr 11 '19 at 05:18
  • 1
    Looks like you're right. Perhaps a problem in importing Observer. In the examples, it is imported from the package "androidx.lifecycle.Observer". My Android Studio inserts the package "java.util.*". – Gregory Apr 11 '19 at 05:38
  • @jeel-vankhede Yes! There was a problem in the import. This code works: pagedListLiveData.observe (lifeCycleOwner, android.arch.lifecycle.Observer { adapter.submitList (it) }) – Gregory Apr 11 '19 at 06:18

3 Answers3

6

Thanks to Kingfisher Phuoc and Jeel Vankhede. Problem was in importing Observer. This code works fine:

pagedListLiveData.observe(lifeCycleOwner, android.arch.lifecycle.Observer{
    adapter.submitList(it)
})
Gregory
  • 575
  • 4
  • 13
0

it's your observer problem. You can just do something like below:

pagedListLiveData.observe(lifeCycleOwner, Observer{
     adapter.submitList(it)
})

Otherwise, you should make sure PagedList<MyItem> not null by using PagedList<MyItem!>!

Kingfisher Phuoc
  • 7,392
  • 8
  • 43
  • 75
0

For those who are here because, you have deleted "Observer" word from LiveData subscriptions before. But something went wrong and after a while Android Studio starts to ask you to bring Observer back. With error like "Type mismatch. Required: Observer Found: () -> Unit"

  1. Go to File->Settings->Languages & Frameworks->Kotlin
  2. Switch "Update chanel" to "Stable"
  3. Click "Check again" button.
  4. Click "Install" button.
  5. Restart Android Studio

P.S. If you don't have Install button, you may also need to switch Update chanel from "Stable" to something else, and then make it "Stable" again.

Kostya Mistery
  • 374
  • 2
  • 9