Giannis Kousis
Giannis Kousis

Reputation: 68

Can't delete node from FirebaseDatabase using removeValue()

I try to delete a node from my FirebaseDatabase with removeValue() but it doesn't work .The strange thing is that when i delete it inside Recyclerview the node deleted successfully .

I pass the ref from Recyclerview with onclick in a new Activity using the code below .

     final DatabaseReference itemRef = getRef(position);
          viewHolder.mView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                //  itemRef.removeValue();
                   Intent intentshowall2one= new Intent(Showcase.this, ShowProduct.class);
                    intentshowall2one.putExtra("S2Skey", itemRef.toString());
                   Showcase.this.startActivity(intentshowall2one);

and on the other activity

    key = myIntent.getStringExtra("S2Skey");
    DatabaseReference  mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl(key);
 mDatabase.removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
     @Override
      public void onSuccess(Void aVoid) {
         Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT)
                                    .show();

           }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
              // Uh-oh, an error occurred!
            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT)
                .show();
              }
        });

Upvotes: 0

Views: 254

Answers (3)

Giannis Kousis
Giannis Kousis

Reputation: 68

Thanks for your answers Lutaaya Huzaifah Idris and Vignesh Kumar.I found out that the error was because i was using Greek words inside my path.Everything worked fine upload download and query the ref with Greek words but when i passing the ref to the other activity the ref changed .That is also strange.

Upvotes: 0

Lutaaya Huzaifah Idris
Lutaaya Huzaifah Idris

Reputation: 3990

Try this out in Your Button or any Listener :

      mDatabase.child(key).removeValue();
            Toast.makeText(getApplicationContext(),"The Post has been removed Successfully  ",Toast.LENGTH_LONG).show();

Or use this

mDatabase.chlid(key).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
         @Override
          public void onSuccess(Void aVoid) {
             Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT)
                                        .show();

               }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                  // Uh-oh, an error occurred!
                Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT)
                    .show();
                  }
            });

Instead of :

mDatabase.removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
     @Override
      public void onSuccess(Void aVoid) {
         Toast.makeText(getApplicationContext(), "deleted ", Toast.LENGTH_SHORT)
                                    .show();

           }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
              // Uh-oh, an error occurred!
            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT)
                .show();
              }
        });

Hope it works for You

Upvotes: 0

Vignesh Kumar
Vignesh Kumar

Reputation: 61

Make sure you have given proper reference before you remove a value. then,

you can use Completion listener instead of OnSuccessListener, Completion listener gives accurate result.

Upvotes: 0

Related Questions