What is the best way to display data from an existing Firestore database in a RecyclerView
using Android?
This isn't covered as a full explanation in an answer, so I've added this Q&A-style so it can be linked to in comments.
What is the best way to display data from an existing Firestore database in a RecyclerView
using Android?
This isn't covered as a full explanation in an answer, so I've added this Q&A-style so it can be linked to in comments.
Assuming you have a Firestore database structure that looks like this:
A model class that looks also like this:
And a
.XML
file that contains aRecyclerView
which also looks like this:To display all the product names, please follow the next steps.
First, you need to find the
RecyclerView
in your activity and set theLinearLayoutManager
like this:Then you need to create the root reference of your Firestore database and a
Query
object like this:Then you'll have to create a
FirestoreRecyclerOptions
object like this:In your activity class, create a
holder
class that looks like this:Then create an
adapter
which is declared as global:And instantiate it in your activity like this:
In the end, don't forget to override the following two methods and start listening for changes:
The result is this:
Edit:
If you want to display a toast message when the user clicks on an item, please add the following lines of code inside the
setProductName()
method from theProductViewHolder
class: