FirebaseUI

Update Queries without changing RecyclerView Adapter using FirebaseUI — Android 🔥

Shreyas Patil
Mar 13, 2020 · 3 min read

In this article, you will learn how to change Firebase Realtime Database/Cloud Firestore queries without changing the whole adapter of a RecyclerView in Android by using the FirebaseUI-Android library.

Let’s say you need to populate a list using RecyclerView to show data loaded from Firebase Realtime Database or Cloud Firestore and you have implemented a custom RecyclerView.Adapter to hold data or you used FirebaseUI-Android which is an official open-source library developed by the Firebase team. In this article, we’re going to use the FirebaseUI-Android library.

We’ll load data from Firebase Realtime Database/Cloud Firestore and populate a RecyclerView (Simple + Paginated) using the adapter. After that, we’ll change/filter the query based on some user interaction at runtime without changing whole adapter.

In the end, you will see app like this 👇. After clicking on one of the buttons, data in RecyclerView will be replaced by new data.

🔥 About FirebaseUI 😃

  • FirebaseUI is an open-source library for Android that allows you to quickly connect common UI elements to Firebase APIs. (see here)
  • It makes it easy to bind data from Firebase Realtime Database or Cloud Firestore to your app’s UI 🎨.

FirebaseUI-Android provides a number of adapters, such as:

  • FirebaseRecyclerAdapter — or binding Firebase Realtime Database
  • FirebaseRecyclerPagingAdapter — for binding Firebase Realtime Database with pagination support
  • FirestoreRecyclerAdapter — for binding Cloud Firestore
  • FirestorePagingAdapter — for binding Cloud Firestore with pagination support

⚡️ Getting Started

Let’s write some code!

Open Android Studio and create a new project. Alternatively, you can simply clone this repository. This is a very simple app for showing a list of posts.

Gradle Setup

In the app module of build.gradle, include following dependencies.

If you’re unsure how to use FirebaseUI 🔥, check out the official source here. Or, take a look at the official sample👇.

⚡️How to Change the Query 🤔

In the adapter classes of FirebaseUI library, there’s a method updateOptions() which initialises an adapter with new options. Whenever this is invoked, the respective RecyclerView is populated with new data.

💻 Let’s Change the Queries 🔥

#1 — Firebase Realtime Database

Here mAdapter is an instance of FirebaseReyclerAdapter. The new instance of FirebaseRecyclerOptions is created with a query: newQuery. Finally, we call updateOptions() . After this, you can observe the change in RecyclerView. 😃

The same can be done in a paging adapter i.e. FirebaseRecyclerPagingAdapter. Just a small change is there while making options is we have to pass PagedList.Config as a parameter along with query in setQuery() .

#2 — Firebase Cloud Firestore

Same as above, here mAdapter is an instance of FirestoreReyclerAdapter.

Again, the same can be done in a paging adapter i.e. FirestorePagingAdapter .

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase…

Sign up for The Firebase Developers Quarterly

By Firebase Developers

A summary of what has happened on the Firebase Developers publication in the past quarter. Sent once a quarter. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.

Shreyas Patil

Written by

📱Android Developer 💻Organizer @KotlinMumbai, 👨‍💻SWE @ScaleReal. More info: http://shreyaspatil.dev

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store