create account

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

View this thread on: hive.blogpeakd.comecency.com
· @faisalamin ·
$22.48
Retrieve Data From Firebase | Android App Development | Lecture#45 | Hive Learners
<center><h2>𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼</h2></center>
<div class="text-justify">

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.

![](https://images.ecency.com/DQmNzbs5dHqzkUCxza6kBuPfdPuyvkrNwTH4gpV5ChdWv1C/multi_purpose_5_.png)


### [GitHub Link](https://github.com/faisalamin9696/HiveLearners2)

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

![](https://images.ecency.com/DQmbyTNrm6b3D17omyUZbQWkLFPdUr3rwKXyQn8xG8VE91w/image.png)


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) {

            }
        });
```

![](https://images.ecency.com/DQmWAmwmn1PucrbyYzbxTF4sphiCfJ64stwXHYxU8yTkkUP/image.png)

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) {

            }
        }); 
```

![](https://images.ecency.com/DQmYfz9bWZ5hQTBTHfYy2CLXNRMbSCCi1fMhG2U11PeYNud/image.png)

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


<center>
![hl_divider.png](https://images.ecency.com/DQmabQycJhW8DT8a634z3wQaVXvPZ5KMii2LYbaWZ3PsaRy/hl_divider.png)

<h2>Thank You</h2>

![hl_footer_banner.png](https://images.ecency.com/DQmZ4HF3hjV4HwJXuw8vRJ6B6CiMC3kUkkoihjtm7z2Gii7/hl_footer_banner.png)
<div class="pull-left"><a href="https://discord.gg/7Bzqv4qUMT">
<img src="https://cdn.steemitimages.com/DQmXd6PwGUHRgSDkWtwKfDvdFpaLHXvXBdK7wnYZeqe1GUa/discord_animation_logo.gif"/></a>
</center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 210 others
properties (23)
authorfaisalamin
permlinkretrieve-data-from-firebase-or
categoryhive-153850
json_metadata{"links":["https://github.com/faisalamin9696/HiveLearners2","https://discord.gg/7Bzqv4qUMT"],"image":["https://images.ecency.com/DQmNzbs5dHqzkUCxza6kBuPfdPuyvkrNwTH4gpV5ChdWv1C/multi_purpose_5_.png","https://images.ecency.com/DQmbyTNrm6b3D17omyUZbQWkLFPdUr3rwKXyQn8xG8VE91w/image.png","https://images.ecency.com/DQmWAmwmn1PucrbyYzbxTF4sphiCfJ64stwXHYxU8yTkkUP/image.png","https://images.ecency.com/DQmYfz9bWZ5hQTBTHfYy2CLXNRMbSCCi1fMhG2U11PeYNud/image.png","https://images.ecency.com/DQmabQycJhW8DT8a634z3wQaVXvPZ5KMii2LYbaWZ3PsaRy/hl_divider.png","https://images.ecency.com/DQmZ4HF3hjV4HwJXuw8vRJ6B6CiMC3kUkkoihjtm7z2Gii7/hl_footer_banner.png","https://cdn.steemitimages.com/DQmXd6PwGUHRgSDkWtwKfDvdFpaLHXvXBdK7wnYZeqe1GUa/discord_animation_logo.gif"],"thumbnails":["https://images.ecency.com/DQmNzbs5dHqzkUCxza6kBuPfdPuyvkrNwTH4gpV5ChdWv1C/multi_purpose_5_.png","https://images.ecency.com/DQmbyTNrm6b3D17omyUZbQWkLFPdUr3rwKXyQn8xG8VE91w/image.png","https://images.ecency.com/DQmWAmwmn1PucrbyYzbxTF4sphiCfJ64stwXHYxU8yTkkUP/image.png","https://images.ecency.com/DQmYfz9bWZ5hQTBTHfYy2CLXNRMbSCCi1fMhG2U11PeYNud/image.png","https://images.ecency.com/DQmabQycJhW8DT8a634z3wQaVXvPZ5KMii2LYbaWZ3PsaRy/hl_divider.png","https://images.ecency.com/DQmZ4HF3hjV4HwJXuw8vRJ6B6CiMC3kUkkoihjtm7z2Gii7/hl_footer_banner.png","https://cdn.steemitimages.com/DQmXd6PwGUHRgSDkWtwKfDvdFpaLHXvXBdK7wnYZeqe1GUa/discord_animation_logo.gif"],"users":["Override","Override","Override","Override","Override"],"tags":["hive-153850","stem","education","ocd","neoxian","development","dev","tutorial","android","firebase"],"description":"","app":"ecency/3.0.25-vision","format":"markdown+html"}
created2022-08-13 16:20:36
last_update2022-08-13 16:20:36
depth0
children1
last_payout2022-08-20 16:20:36
cashout_time1969-12-31 23:59:59
total_payout_value11.280 HBD
curator_payout_value11.198 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,689
author_reputation104,701,778,605,493
root_title"Retrieve Data From Firebase | Android App Development | Lecture#45 | Hive Learners"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id115,689,831
net_rshares30,302,449,326,103
author_curate_reward""
vote details (274)
@stemsocial ·
re-faisalamin-retrieve-data-from-firebase-or-20220813t174129900z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> 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.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-faisalamin-retrieve-data-from-firebase-or-20220813t174129900z
categoryhive-153850
json_metadata{"app":"STEMsocial"}
created2022-08-13 17:41:30
last_update2022-08-13 17:41:30
depth1
children0
last_payout2022-08-20 17:41:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,459,343,683,868
root_title"Retrieve Data From Firebase | Android App Development | Lecture#45 | Hive Learners"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id115,691,686
net_rshares0