Update Queries without changing RecyclerView Adapter using FirebaseUI — Android 🔥
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.
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👇.
A collection of quickstart samples demonstrating the Firebase APIs on Android. For more information, see…
⚡️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
mAdapter is an instance of
new instance of
FirebaseRecyclerOptions is created with a query:
newQuery. Finally, we call
updateOptions() . After this, you can observe the change in
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
#2 — Firebase Cloud Firestore
Same as above, here
mAdapter is an instance of
Again, the same can be done in a paging adapter i.e.
We have successfully changed queries at runtime using the FirebaseUI library😃.
The source code for this article is available in this GitHub repo.
Thank You! 😃
Example app to demonstrate change query of Firebase/Firestore in RecyclerView without changing the whole adapter. …