Muhammad Faisal Amin avatar

Update, Delete Firestore Data | Android App Development | Lecture#56 | Hive Learners

faisalamin

Published: 26 Aug 2022 › Updated: 26 Aug 2022Update, Delete Firestore Data | Android App Development | Lecture#56 | Hive Learners

Update, Delete Firestore Data | Android App Development | Lecture#56 | Hive Learners

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Hello, Dear Hive Learners, In the previous lecture we cover how to retrieve the data from the Firebase Firestore and we show this data in a listview. Today, we will learn how to delete or update the data in a Firestore collection. To update and delete the specific document we need the document id and we already store it inside the document. We can use the modal class o get the document id from the list and delete that item. 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 Delete or update data in Firestore

Assignment

  • Perform Deletion and Updation operations on the Firestore database.

Procedure

In our adapter class, we have the delete button and click listener. We have already implemented the Real-time database deletion code in it. But now we will remove the code inside the listener and we will write the code for the Firestore to delete the clicked document.


                    FirebaseFirestore.getInstance().collection("sendings").document(delete_child).delete().addOnCompleteListener(task -> {
                        if (task.isSuccessful()) {
                            Toast.makeText(mContext, "Deleted", Toast.LENGTH_SHORT).show();
                            this.notifyDataSetChanged();

                        } else
                            Toast.makeText(mContext, "Failed to delete", Toast.LENGTH_SHORT).show();
                    });

alt

It will delete the targeted document from the database and send a deleted message on the screen. Now the time we also need to update the data in a document. We will implement an update button in the transfers_fragment class and an EditText that we will use to enter the document id that we need to update. This time we will update the document manually but in the next lecture, we will add an update button with our adapter class.

alt

Declare and initialize these new EditText and button in the java class and implement the on Click listener.

alt

Now we need to write the code to get the values from the account_num_et`` andamount_etfields. We will store the values in a hashmap and use the hashmap to update a document with the document id got from thedoc_id_et``` field.

update_doc_btn.setOnClickListener(v -> {
            HashMap<String, Object> hashMap = new HashMap<String, Object>();

            hashMap.put("account", account_num_et.getText().toString().trim());
            hashMap.put("amount", Float.parseFloat(amount_et.getText().toString().trim()));
            firestore.collection("sendings").document(doc_id_et.getText().toString().trim()).update(hashMap).addOnCompleteListener(task -> {
                if (task.isSuccessful()) {

                    Toast.makeText(requireContext(), "Updated", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(requireContext(), "Failed", Toast.LENGTH_SHORT).show();
                }

            });


        });

alt

I found an error in the Adapter class of null pushKey value. We forget to change the variable name in the POJO class from pushKey to doc_id. Let's do it and make a change in the Adapter class too.

alt

alt

alt

Let's run the app and test the delete and update functionality.

Value is deleted but changing is not showing as we have the one-time listener adapter we will fix it in the coming lectures.

alt

Let's perform the updation by getting the id from the database manually.

alt

hl_divider.png

Thank You

hl_footer_banner.png

Leave Update, Delete Firestore Data | Android App Development | Lecture#56 | 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