Search code examples
androidfirebasegoogle-cloud-firestorefirebaseui

How to Update Query for Firebase UI recycler view adapter?


I am using Firebase UI for displaying data in recyler view from Firestore. Currently I am setting up the query and passing it to the recycler view once in the beginning. Now I want to apply some filter for which the query needs to be changed. I look at the official documentation and couldn't find something like that. I have followed this https://github.com/firebase/FirebaseUI-Android/blob/master/firestore/README.md

This is what have done so far

 val query = db.collection("items")
        .whereEqualTo("inStock",true)
        .limit(10)

    val options = FirestoreRecyclerOptions.Builder<ItemModel>()
        .setQuery(query, ItemModel::class.java)
        .setLifecycleOwner(this)
        .build()

    val adapter = FirestoreQuoteAdapter(options)
    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(this@MainActivity)

Based on user interaction, I would like to change Query to something like this, so that my recylcer view gets filtered according to the new query.

val query = db.collection("items")
        .whereEqualTo("inStock",false)
        .limit(10)

Can anyone please guide me on this?


Solution

  • I made this update in my graddle and the updateOptions method became available. make sure in you gradle module app you have at least these versions or higher:

    implementation'com.firebaseui:firebase-ui-firestore:6.3.0'
    implementation 'com.firebaseui:firebase-ui-storage:6.3.0'
    

    then do this:

    val newOptions = FirestoreRecyclerOptions.Builder<ItemModel>()
        .setQuery(newQuery, ItemModel::class.java)
        .setLifecycleOwner(this)
        .build()
    adapter.updateOptions(newOptions);