Android App Development | Lecture#41 | Hive Learners

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Hello dear Hive Learners, In lecture 40 we configure the Firebase database in our project and we are ready to use it. Today we will send some data to firebase's real-time database. Firebase allows using the tree structure. As firebase is a NoSQL database. We have a key/value in the tree structure. We can also assume it is a parent-child relationship. So let's get started.

GitHub Link

Use this GitHub project to clone into your directory. The following lecture will constantly update it so you will never miss the latest code. Happy Coding!

What Should I Learn

  • How to send Data to Firebase real-time database
  • How to use HashMap

Assignment

  • Save data to Firebase real-time database.

Procedure

We are creating a new activity where we add 2 Edittext to get in our from the user and a button to send the data to the firebase database. I create an activity Transer_Funds_Activity and there I add my design in the XML file.

Now we need to declare and initialize them in the java file and also add the click listener for the button.

Now we need to add the Firebase Database and Database Reference and also initialize it. I have created a Reference sending. It means the parent is now sending and data will store in this parent as a child.

Now we will also add a ProgressDialog and initialize it and then we need to set the checks in the click listener of the send button.


        send_btn.setOnClickListener(v -> {
            // Button Click Listener
            String account = account_num_et.getText().toString().trim();
            String amount = amount_et.getText().toString().trim();
            if (account.isEmpty() || amount.isEmpty()){
                Toast.makeText(this, "Some field are empty", Toast.LENGTH_SHORT).show();
                return;
            }
            

        });

Now we need to add the data to the HashMap. You can consider it as name and value matching. We will add the Name and Value in the hashmap so that it can be saved the same as in the database.

            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put("account", account);
            hashMap.put("amount", amount);

Now we will add the code to send data to firebase and show a success or fail message, We will also show the ProgressDialog during the process. If the

On the Welcome screen, I use the Transfer Card to navigate to the Transfer_Funds_Activity.

We also need to create a real-time database in the firebase console. Follow these steps.

Let's run and save and check if the data is saving or not.


hl_divider.png

Thank You

hl_footer_banner.png



0
0
0.000
2 comments
avatar

Wow this is good I’ve always wanted to learn android development

0
0
0.000
avatar

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. 
 

0
0
0.000