Delete Specific Parents Firebase | Android App Development | Lecture#48 | Hive Learners

𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼

Hello Hive Learners, In the 47th lecture we learn how to delete data in the firebase. We learn if we delete the parent-child the values inside it will delete automatically. We simply learn how to remove the data with a fixed parent. But, today we will learn how we can add more than one child with different parent keys and how to delete those parents.

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 add multiple parents.
  • How to store the Parent key as a child
  • Delete a parent

Assignment

  • Delete a parent using the pushKey child.

Procedure

First, we need to add the Firebase push key to a variable. Then we will use this variable as a parent and also store it as a child.

            String push_key = sendings_ref.push().getKey();

We also need to store this push key in our database. For that, we need to add our hashmap.

            hashMap.put("pushKey", push_key);

Now we need to use this push key as a parent to store the data in database reference.

 sendings_ref.child(push_key).setValue(hashMap).addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    if (progressDialog.isShowing())
                        progressDialog.cancel();

                    Toast.makeText(requireContext(), "Data Added", Toast.LENGTH_SHORT).show();
                } else {

                    if (progressDialog.isShowing())
                        progressDialog.cancel();
                    Toast.makeText(requireContext(), "Failed", Toast.LENGTH_SHORT).show();
                }
                //41 lecture end
            });

Now add this pushKey variable in the POJO class and add the getter and setter listener in the class by pressing Alt + Insert.

package com.example.hivelearners2;

public class MyList_POJO {
    String title, subTitle, pushKey;

    public MyList_POJO(String title, String subTitle) {
        this.title = title;
        this.subTitle = subTitle;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSubTitle() {
        return subTitle;
    }

    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
    }

    public String getPushKey() {
        return pushKey;
    }

    public void setPushKey(String pushKey) {
        this.pushKey = pushKey;
    }
}

Go to the Adapter class and change the delet_child variable with the getter of this pushKey.

We need to add fresh data in the Firebase so we will delete all the database data by deleting the parent reference manually by going to the Firebase Database console.

Now add new data by using the Transfers_Fragment.

Check in the Firebase if we are getting the same parent and pushKey child.

Add one more data and perform the delete process on a single one to test if it is working or not.

We get an error of null child to remove. It means we are not getting any value in the pushKey variable in the POJO class. We haven't added the constructor in the POJO class and we also need to add the value in the constructor in the for loop of Blogs_Fragment. Let's do it.

Let's try again.

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center