Delete Firebase Data | Android App Development | Lecture#47 | Hive Learners
𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
Hi dear Hive Learners, I hope you all are well. In lecture 46 we learn how to process the retrieved data from the firebase real-time database and then we add this data in a model class and use this model class in an adapter to show the data in a listview. We also successfully run the app and it was working like charm. Today we will learn how to delete data from the firebase database by using the button in the listview. So 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 delete data from Firebase real-time database
Assignment
- Delete data on a button click
Procedure
We already has a delete button in our listview and also have a listener for it. We need to get to know what child we are deleting and write the code according to it. We will also implement the complete listener to sow delete successful or failed messages on the screen. We implement the listener interface in the Fragment When we move the code. That interface will not work in the Fragment so we will use the onclicListenere in the Adapter class.
Now we need to first get the child that we want to delete on the button click. We want the parent node and the child inside it can be deleted automatically. We are using the Firebase Auth id as the parent key. So we can get and use the Firebase uid. Using parent Firebase uid will not allow us to store more than one data for a user. We need to use the PushKey and we will do it in the next lecture. This time we are simply learning how to delete the data in firebase.
We can implement the task complete listener to show the success or failure message. I add the new data in the database so we can save the data via uid.
We hardcode the username value which is why we get the My Account in the username field. Here we now have two children in the sending reference.
Now press the delete button on this child and check if the data is deleted from the database or not.
Data is deleted but we have not notify the adapter that data is changed so we need to do so. We also need to change the Listener type from Single Listener to a regular Listener so that the database can get the change and update the POJO class. I also change the code in the Transfers_Fragment to store the account_number correctly.
Let's try to add and delete the item again.
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.
!1UP I have a friend who is having a lot of problems with creating apps, I will recommend his profile to those who know how to solve many of his problems 😅
Click this banner to join "The Cartel" discord server to know more.
You have received a 1UP from @underlock!
@stem-curator, @neoxag-curator
And they will bring !PIZZA 🍕.
Learn more about our delegation service to earn daily rewards. Join the Cartel on Discord.