Search code examples
javaandroid-livedataandroid-architecture-componentsmutablelivedata

Observe list of live data in java


I have a list of liveData, like List<LiveData<String>>. I want to observe this list so that whenever the String inside the LiveData changes, it would notify the observer of the list with this String.
How can I do this?
My list elements are strictly LiveData<String> so, it can not be simplified to String.
I know how to observe LiveData<String>, but not List<LiveData<String>>.
Please help.

Edit:

The element of the list is LiveData<String> because the String it is coming from the internet. So, list elements are holding object of LiveData<String>.


Solution

  • If you want to observe a List<LiveData<String>>, you would have to attach an observer to each element of the list. The list itself is not observable, only each LiveData element of it. So, simply:

    1. Iterate the List
    2. Observe each LiveData element
    3. Decide what you want to do for each observed element