Retrieve Data From Firebase | Android App Development | Lecture#45 | Hive Learners

in Hive Learners • 2 months ago

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Hello Hive Learners community members, I hope you all are well. Yesterday we learned how to use fragments and we moved all the code from activities to chips. Now we have two fragments in the Tab Layout of two items on the Welcome screen. Today we will learn how to get data from firebase by using different methods.

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

  • Retrieve Data from Firebase Database

Assignment

  • Retrieve Data from Firebase and show it on screen.

Procedure

We are going to retrieve data from the firebase that we have stored in sendings database references. Write this code in Blogs Fragment and set the listeners. This listener will work only once if it failed on the startup it will not run again automatically.

sendings_ref = database.getReference("sendings");
        sendings_ref.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DataSnapshot> task) {
                Toast.makeText(requireContext(), task.getResult().getValue().toString(), Toast.LENGTH_SHORT).show();
            }
        });

There are different kinds of listeners, We can get data in Hashmap or JSON array, or we can use a model class to get the data in it. We can use the always listener or a single listener that will listner only for one time. addValueEventListener will always listen for any change in the target directory. It will auto-update the changing on screen when a user update, insert, or delete data from that directory.

 sendings_ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                Toast.makeText(requireContext(), snapshot.getValue().toString(), Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

We can also listen for one-time in changes in data in the directory by using addListenerForSingleValueEvent.

 sendings_ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                Toast.makeText(requireContext(), snapshot.getValue().toString(), Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        }); 

So we learn how to get data from the Firebase database, Tomorrow we will process this data and use it.


hl_divider.png

Thank You

hl_footer_banner.png

Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.