Deleting auto key node from firebase android

Viewed 582

I'm not sure if I'm doing this right but I'm creating a Query to get the auto-generated key that is stored in firebase. I want to delete this key from firebase. I'm wondering if this the right process? Like is it possible to just call removeValue() on the query or have to use Datasnapshot? My code is as follows. All it does is gets the auto-generated key and prints it.

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); String removeQuery = ref.child("EventData").push().getKey(); System.out.println(removeQuery); Log.d(TAG,"Remove Query was called !!!!!!!!!!!!");

Updated1 enter image description here

Updated2

Is there an easy way to set removeQuery to null?

2 Answers

Here:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 
String removeQuery = ref.child("EventData").push().getKey();

You are creating a new key and storing it inside a variable. You can do that in the beginning (when storing data to the database), add data to Intent:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 
String removeQuery = ref.child("EventData").push().getKey();
Intent intent = new Intent(getBaseContext(), Activity.class);
intent.putExtra("key", removeQuery);
startActivity(intent);

then later in the other activity you can delete like this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); 
String key = getIntent().getStringExtra("key");
ref.child("EventData").child(key).removeValue();

You have to store the pushID which is M1VA.... and then you can delete by like this. Refer to this link: https://firebase.google.com/docs/database/android/read-and-write#add_a_completion_callback

mDatabase.child("EventData").child("M1VA....").removeValue()
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // Write was successful!
                // ...
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Write failed
                // ...
            }
        });
Related