𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
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.
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();
});
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.
Declare and initialize these new EditText and button in the java class and implement the on Click listener.
Now we need to write the code to get the values from the account_num_et`` and
amount_etfields. We will store the values in a hashmap and use the hashmap to update a document with the document id got from the
doc_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();
}
});
});
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.
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.
Let's perform the updation by getting the id from the database manually.

Thank You
