Answer the question
In order to leave comments, you need to log in
How to work with LiveData on Kotlin?
There is a Java code in which a subscription to LiveData is carried out and the "onChanged" function is called there without problems:
And here is the code that does the same in Kotlin, but the "onChanged" function does not want to be called:
Please tell me where is the error?
Java project: Link1
Link2
Kotlin project: Link1
Link2
Answer the question
In order to leave comments, you need to log in
remove this ListView initialization line from the onChanged method. The method is needed only to update the data. This is how I do it myself
viewModel.getItems().observe(this, Observer { collections ->
galleryRecycler.visibility = View.VISIBLE
progressBarGallery.visibility = View.GONE
galleryAdapter.setItems(collections.mapToGalleryItem())
})
Thank you very much! Replaced a piece of code:
RecsLiveData.observe(this, Observer<List<Rec>>() {
fun onChanged(@Nullable Recs: List<Rec>) {
val lvMain: ListView = findViewById(R.id.lvMain) as ListView // создаем адаптер
recAdapter = RecAdapter(this@MainActivity, Recs)
lvMain.setAdapter(recAdapter) // присваиваем адаптер списку
}
RecsLiveData.observe(this, Observer<List<Rec>>() {
val lvMain: ListView = findViewById(R.id.lvMain) as ListView // создаем адаптер
recAdapter = RecAdapter(this@MainActivity, it)
lvMain.setAdapter(recAdapter) // присваиваем адаптер списку
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question