-2

I went through various links and solutions delete-child-from firebase, How to remove child nodes and try to use in my project but nothing works.As following the link Deletion of child nodes,

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("calendario").child("-L7jrJ6DtQWrmZsC4zvT").removeValue();

it works on the case where key is not generated which is not mine case.Database Structure is here.

enter image description here

Following piece of Code is for deletion of individual child which I have tried. Any help is appreciated.

 delete.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View view) {


            FirebaseDatabase ref = FirebaseDatabase.getInstance();
            DatabaseReference myRef = ref.getReference("Students");
            String key = myRef.push().getKey();
             myRef.child(key).removeValue(); 
        }
    });
SM123
  • 25
  • 1
  • 11
  • is your code not deleting the push key? – Peter Haddad Mar 18 '19 at 12:43
  • I am a little confused. `push()` will create a new key and `removeValue` will delete it. You have to pass the key that you want to delete somewhere. – vavasthi Mar 18 '19 at 12:45
  • @PeterHaddad yes Sir – SM123 Mar 18 '19 at 12:48
  • @VinayAvasthi Sir,how can I modify this code? – SM123 Mar 18 '19 at 12:49
  • @VinayAvasthi I have tried to delete like this, but not having deletion ,help me.https://codeshare.io/5MPWKQ – SM123 Mar 19 '19 at 14:23
  • You have to tell how are you populating the values. I assume `delete` is a button. On the UI how does the user select what to delete? Are you using RecyclerView or something else? – vavasthi Mar 19 '19 at 16:25
  • @VinayAvasthi Sir, that is delete button and I am using RecyclerView.I cannot solved this till today,the attached link contains the code of adapter class.I am new to firebase.https://codeshare.io/5X08Bz – SM123 Mar 19 '19 at 16:46
  • You should keep the key in CustomAdapter and MyHolder classes. I see that you have a Student class. What I prefer is that I also keep the key in Student class. That way when you are registering the listener for `delete` you will have access to the key. The CustomAdapter has a list of Students, you add key as attribute in Student and populate when you create a new item. – vavasthi Mar 20 '19 at 03:55

3 Answers3

1

When you use push(), you are generating a new random id, then when you use removeValue() on that key it will lead to deleting that key.

If you want to delete an existing key in your database, then you need to retrieve it from the database to be able to use it with removeValue().

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
1

You aren't deleting anything because you are only generating a new key. If you want to delete an existing key, please use the following line of code:

ref.getReference("Students").child("-LaFeagvdt-mLlc2eWeV").removeValue();

The result will be the deletion of the second record.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Sir,keys are generated in random ways,so it might be a difficult job to store every keys which are randomly generated, and store them in the variable form ,for the deletion options.Is there other ways ?? – SM123 Mar 18 '19 at 13:22
  • 1
    Check **[this](https://stackoverflow.com/questions/51787784/how-to-get-specific-pushedid-in-firebase/51788244)** out. – Alex Mamo Mar 18 '19 at 13:29
  • This updates the randomly generated id but I cannot know how to use this for deletion help me!.https://codeshare.io/5DNgNl – SM123 Mar 19 '19 at 04:31
  • (I have tried this code for deletion but didnot work). DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); String studentId = rootRef.child("Students").push().getKey(); Map map = new HashMap<>(); map.put("studentId", studentId); rootRef.child("Students").child(studentId).removeValue(); – SM123 Mar 19 '19 at 04:40
  • Again I have tried to delete like this, but not having deletion ,help me.https://codeshare.io/5MPWKQ – SM123 Mar 19 '19 at 05:23
0

You need to keep the key of the child you want to delete in some variable. Let's assume it is in a variable keyToBeDeleted.

String keyToBeDeleted = "-LaFeagvdt-mLlc2eWeV";
 delete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


        FirebaseDatabase ref = FirebaseDatabase.getInstance();
        DatabaseReference myRef = ref.getReference("Students/" + keyToBeDeleted);
        myRef.removeValue();
    }
});
vavasthi
  • 922
  • 5
  • 14
  • Sir,keys are generated in random ways,so it might be a difficult job to store every keys which are randomly generated, and store them in the variable form ,for the deletion options.Is there other ways ?? – SM123 Mar 18 '19 at 13:18
  • 1
    You need to keep track of which key you want to delete. When You are listing items for deletion, at that time you will have access to those keys and then depending on which button you click on, you need to use that key to delete it. – vavasthi Mar 18 '19 at 13:20
  • Would you please suggest me on programs, to implement this logic? – SM123 Mar 18 '19 at 13:25