Android Load FirestoreUI FirestoreRecyclerAdapter With Empty Query (Clear RecyclerView)

April 8, 2019

I am using FirestoreRecyclerAdapter to load firestore query into RecyclerView.

Why need empty query/empty recycler view?

I already loaded RecyclerView with Query data when user signin, then I need to empty/clear the RecyclerView when user signout.

  • I could hide the RecyclerView
  • Or create a Firestore Query which I am sure will not return any result.
  • Or I can create a blank RecyclerView.Adapter where getItemCount() return 0.
  • Or I can use FirestoreRecyclerOptions.Builder#setSnapshotArray (instead of setQuery) and assign an empty snapshot array.


import com.firebase.ui.firestore.ObservableSnapshotArray
import com.firebase.ui.firestore.SnapshotParser

class EmptySnapshotArray<T>: ObservableSnapshotArray<T>(SnapshotParser<T> {
}) {
    // class EmptyArray<T>(parser: SnapshotParser<T>): ObservableSnapshotArray<T>(parser) {
    override fun getSnapshots(): MutableList<DocumentSnapshot> {
        return mutableListOf()
class MyActivity:  AppCompatActivity() {

    private lateinit var adapter: LocalAdapter

    private fun createAdapter(): LocalAdapter {
        if (::adapter.isInitialized) {
            // call this before replacing the adapter to prevent invalid snapshot

        // query=null when user not signin
        val query = ...

        // Quote is Model
        val builder = FirestoreRecyclerOptions.Builder<Quote>()

        if (query != null) {
            builder.setQuery(query, object : SnapshotParser<Quote> {
                override fun parseSnapshot(snapshot: DocumentSnapshot): Quote {
                    return Quote.toObject(snapshot)!!.also {
        else {
            // no query, set empty

        val options =
        return FirestoreQuoteAdapter(options, viewModel).also {
            adapter = it

    private fun initUi() {
        recyclerView.adapter = createAdapter()

NOTE: Refer to Android Firestore RecyclerView With FirestoreUI FirestoreRecyclerAdapter for Quote and FirestoreQuoteAdapter implementation.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.