Muhammad Faisal Amin avatar

Retrieve Firestore Data | Android App Development | Lecture#55 | Hive Learners

faisalamin

Published: 25 Aug 2022 › Updated: 25 Aug 2022Retrieve Firestore Data | Android App Development | Lecture#55 | Hive Learners

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.

alt

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.

alt

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.

alt

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.

alt

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();


        });

alt

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.

alt

hl_divider.png

Thank You

hl_footer_banner.png

Leave Retrieve Firestore Data | Android App Development | Lecture#55 | Hive Learners to:

Written by

Android | Python | Software Engineer

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