Retrieve Firestore Data | Android App Development | Lecture#55 | Hive Learners
𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
Hello, dear Hive Learners, I hope you all are well. In the previous lecture, we learn how to store data in the Firebase Firestore database. We save the data in a collection named sendings and we use a document id we store data in the document and also store this document id with our hashmap. So today we will learn how to retrieve the data from the Firebase Firestore. Let's get started.
GitHub Link
Use this GitHub project to clone into your directory. The following lecture will update it so you will never miss the latest code. Happy Coding!
What Should I Learn
- How to retrieve data from Firestore
Assignment
- Retrieve data from Firestore in a list
Procedure
We will retrieve the data in the Blogs_Fragment.java activity and we are going to replace the real-time database code. Declare and initialize the Firestore.
To retrieve the data we need to open the same collection of Firestore and we get all the documents in it we can loop through all the documents as we do in the real-time database to get all the saved values in the model class.
We need a for-each loop in the listener. In the listener first, we will check if the task is successful or not and in the successful condition we will write our code and in the else part we will show the failed message.
Now in this for-each loop, we can move the same real-time database loop code with some modification in getting values. We will get the values from the DocumentSnapshot ds and store them in the variables then we use these variables to set it in the model class and outside the for loop, we will notify the adapter that data is changed and need to load the fresh data.
firestore.collection("sendings").get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
myList_pojos.clear();
for (DocumentSnapshot ds : task.getResult()) {
String username = ds.get("account", String.class);
Float amount = ds.get("amount", Float.class);
String pushKey = ds.get("pushKey", String.class);
myList_pojos.add(new MyList_POJO(username, String.valueOf(amount), pushKey));
}
customAdapter.notifyDataSetChanged();
} else Toast.makeText(requireContext(), "Fetching failed", Toast.LENGTH_SHORT).show();
});
Let's Run the app and check if the data is loading or not. I am using the real device as the emulator that is not connecting with the internet for some reason. I will install a new emulator later. Also, remove the real-time database code sp there will be no conflict.
Thank You
Leave Retrieve Firestore Data | Android App Development | Lecture#55 | Hive Learners to:
Read more #hive-153850 posts
Best Posts From Muhammad Faisal Amin
We have not curated any of faisalamin's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Muhammad Faisal Amin
- Best Family Relationships Practices
- Grocery for creamy fruitĀ chat
- Shopping in Rainy Day
- Delicious Chinese food withĀ family
- Protest in my City
- Attended PTM of my cusion
- Delicious job treat from belovedĀ friend
- Visited nearby school for invigilation of scienceĀ projects
- Water quality monitoring at my universityĀ lab
- First ride in my Friends's newĀ car