1

There are multiple RecyclerView in my application. Each one consists of the same records, but with different filters. For example, the first RecyclerView contains new records, the second RecyclerView contains the most popular, etc.

I am trying to get "voices" with different filters. But in the end I get 2 identical lists.

My ViewModel:

private var recentlyAddedVoices = MutableLiveData<List<VoicesModel>>()
private val topFreeVoices = MutableLiveData<List<VoicesModel>>()


private val favExists = MutableLiveData<Boolean>()

private val addToFavoriteResult = MutableLiveData<Boolean>()


val homeVoicesData: MutableLiveData<Pair<List<VoicesModel>?, List<VoicesModel>?>> =
        object: MediatorLiveData<Pair<List<VoicesModel>?, List<VoicesModel>?>>() {

            var voices: List<VoicesModel>? = null
            var freeVoices: List<VoicesModel>? = null

            init {
                addSource(recentlyAddedVoices) { voices ->
                    this.voices = voices
                    voices?.let { value = voices to it }
                }
                addSource(topFreeVoices) { free ->
                    this.freeVoices = free
                    freeVoices?.let { value = freeVoices to it }
                }
            }
        }


fun loadRecentlyAddedVoices(){
    REF_DATABASE_ROOT.child(NODE_STICKERS).addValueEventListener(object :
            ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {

            val tmpList: MutableList<VoicesModel> = mutableListOf()
            for (ds in snapshot.children) {
                val voices: VoicesModel? = ds.getValue(VoicesModel::class.java)
                voices!!.pushKey = ds.key.toString()

                tmpList.add(voices)
            }
            recentlyAddedVoices.postValue(tmpList)
        }

        override fun onCancelled(error: DatabaseError) {

        }

    })
}


fun loadTopFree(){
    REF_DATABASE_ROOT.child(NODE_STICKERS).
    orderByChild(CHILD_IS_FREE).
            equalTo(true).
    addValueEventListener(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            val tmpList: MutableList<VoicesModel> = mutableListOf()
            for (ds in snapshot.children) {
                val voices: VoicesModel? = ds.getValue(VoicesModel::class.java)
                voices!!.pushKey = ds.key.toString()

                tmpList.add(voices)
            }
            topFreeVoices.postValue(tmpList)
        }

        override fun onCancelled(error: DatabaseError) {

        }

    })
}

Observe in Fragment:

firebaseViewModel.homeVoicesData.observe(this){ (recentlyAdded, topFree) ->
        // recentlyAdded and topFree equals identical value
        UpdateUI()
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • you should separate them into different `livedata` and have a `setter` in your recyclerview so that your recyclerview can be updated based on the response. – Teo Jul 29 '21 at 03:31
  • Why do you say that are identical? I see in your code two different queries? – Alex Mamo Jul 29 '21 at 07:52

0 Answers0