Skip to content Skip to sidebar Skip to footer

Delete Element In Firebase With Android

I have a firebase data structure which looks like this: |---employees |----KJSXd4ScEmJ6N4UFc5k |---employeeName:'semah' |---employeeAge:'24'

Solution 1:

Since you don't have the reference to the key KJSXd4ScEmJ6N4UFc5k, you need to retrieve the value and try deleting it with the ref of value retrieved. Something like this

 database.getReference("employees").orderByChild("employeeName").equalTo("semah").addListenerForSingleValueEvent(
                newValueEventListener() {
                    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshotchild: dataSnapshot.getChildren()) {
                child.getRef().setValue(null);
            }
        }


       @OverridepublicvoidonCancelled(DatabaseError databaseError) {
              Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
         }
});

Solution 2:

In your case you can build a query to retrieve the item to delete. Something like:

Firebase refEmployees = newFirebase("..../employees");
Query queryRef = refEmployees.orderByChild("name").equalTo("semah");


queryRef..addListenerForSingleValueEvent(
            newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
             String key=snapshot.getKey(); 
             //Remove the item
        }


       @OverridepublicvoidonCancelled(DatabaseError databaseError) {

         }
});

Post a Comment for "Delete Element In Firebase With Android"