In Android, I have a ListAdapter that I'm passing callbacks to using Kotlin's lambda notation.
For instance, the ListAdapter has:
var clickCallback = { _: Long -> Unit }
Which gets set in the Fragment calling the ListAdapter with:
mAdapter.clickCallback = {id: Long -> selectItem(id)}
This Fragment function selectItem(id: Long) then gets called inside the ListAdapter with:
clickCallback(item.id)
But what if that passed function selectItem() returned a value, such as a LiveData, and what if I wanted to call that function and observe that LiveData from inside the adapter? I cannot for the life of me figure out how to pass such a function, so that the callback function inside the ListAdapter can be observed.
This doesn't even come close to working, but hopefully it should give you an idea of the sort of solution I'm looking for:
var clickCallback:LiveData<String> = { _: Long -> Unit?? }
....
clickCallback(item.id).observe(holder.ItemView, Observer { answerString ->
doSomethingTo(answerString)
})
Bonus points if you know how to get the viewLifecycleOwner inside a ListAdapter. I'm just guessing it's the holder.ItemView.
Thanks!
John
{}but Probably you wanted to do:clickCallback: (Long) -> LiveData<String>.