Remove Specific value From Firebase Database - android

I want to Delete Specific value from Firebase Database following is Database which i have stored
Now, I want to Delete Following Values
fname: "John"
lname: "wick"
Here, What i have try to delete specific value from Firebase..
DatabaseReference demo = mReference.child("demo").orderByValue().equalTo("John").getRef();
demo.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But this will delete all values from database...
Question : How to delete that specific value??? what i have to do for that
Any help will be appreciated.

Instead of using addListenerForSingleValueEventtry using addChildEventListener as below,
Query queryRef = mReference.child("demo").orderByChild("fname").equalTo("John");
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot snapshot, String previousChild) {
snapshot.getRef().setValue(null);
}
});

hi try using this code,
mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
}
});

You can try it:
mReference.child("demo").orderByChild("fname").equalTo("John").addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().setValue(null);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
}
});

Well Thanks to all, After Modification with lots of answer and query.. This is what worked with me.
mReference.child("demo").orderByChild("fname").equalTo("John").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
mReference.child("demo").child(dataSnapshot.getKey()).setValue(null);
//also work
//dataSnapshot.getRef().setValue(null);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Try this code. Hopefully this will help you
Thanks
database.getReference().child("Groups").child(groupItem.getGroupkey()).child("members").child(groupMember.getKey()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
new CustomMessage(GroupDetailsActivity.this, "Successfully deleted");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Customlog.showlogD("ERROR_MSG",e.getMessage());
}
});
Database structure: https://i.stack.imgur.com/Lw3Ia.png

Related

how to get data from firebase database?

how can i get data from firebase Realtimedatabase?
i very new on Android an have no plan how to do this. can someone suggest me?
is this ok?
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
Post newPost = dataSnapshot.getValue(Post.class);
System.out.println("Author: " + newPost.author);
System.out.println("Title: " + newPost.title);
System.out.println("Previous Post ID: " + prevChildKey);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
you can use this:
DatabaseReference.getInstance().getReference("user").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Child child = dataSnapshot.getValue(Child.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Delete a child from firebase android?

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
// Toast.makeText(Map.this, marker.getPosition()+"", Toast.LENGTH_SHORT).show();
Query query = databaseReferenceMarkers.orderByChild("lat").equalTo(marker.getPosition().latitude);
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Toast.makeText(Map.this, "test", Toast.LENGTH_SHORT).show();
dataSnapshot.getRef().removeValue();
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return true;
}
});
Why am I not able to delete the marker?
onChildRemoved() -- no response.
The problem is that you're adding a value change listener, so onChildRemoved() will only be called once you remove the child, but you're removing it in the callback function.
Try it like this:
Query query = databaseReferenceMarkers.orderByChild("lat").equalTo(marker.getPosition().latitude);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) { child.getRef().removeValue(); }
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
The query returns a collection of datasnapshots where there is a match. Then you'd need to iterate over them deleting every one of them.

How to get User id from Firebase in android?

I have Done this:
adapter.getRef(currentPosition).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
adapterKey = dataSnapshot.getChildren().toString();
Log.i("adapterKey",adapterKey);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to retrieve the Id's generated by firebase in order to access the inner data to EndUsers node. How can I do that?
Try like this
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
//snapshot.getKey()
//snapshot.getValue()
}
DatabaseReference mDatabaseReference; //you must initialize it
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
Log.i(TAG, "the key is : " + child.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

Android - Firebase: Delete all child with specific id

can somebody help me please i cant get it working.
The Database structure looks like:
-Abbos
1509917361
ELtRM6aTs9XYkMmdrDZ8gRxpgec2
id: "ELtRM6aTs9XYkMmdrDZ8gRxpgec2"
sortorder: -1509917361
FLARM6aTs9XYkMmdrDZ8gRxpgeh4
id: "FLARM6aTs9XYkMmdrDZ8gRxpgeh4"
sortorder: -1509917361
1509917780
ELtRM6aTs9XYkMmdrDZ8gRxpgec2
id: "ELtRM6aTs9XYkMmdrDZ8gRxpgec2"
sortorder: -1509917780
Now i want to remove all entries with eg the id "ELtRM6aTs9XYkMmdrDZ8gRxpgec2"
but my code doesnt work.
Query queryRef = mUserAbosDB.child("Abbos").orderByChild("id").equalTo(current_user_id);
queryRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
dataSnapshot.getRef().setValue(null);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thanks in advance
Use this-
DatabaseReference drTest = FirebaseDatabase.getInstance().getReference();
drTest.child("Abbos").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot:snapshot.getChildren()) {
if(dataSnapshot.hasChild(id)){
dataSnapshot.getRef().setValue(null);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG: ", databaseError.getMessage());
}
});
i got it :)
DatabaseReference drTest = FirebaseDatabase.getInstance().getReference();
drTest.child("Abbos").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
areaSnapshot.child(current_user_id).getRef().setValue(null);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thank you Arif Khan

Android - Firebase many to many relationship child listener

My firebase database has the following structure:
tasks
- task_key_1:{
- name: "task 1"
- ...
- users{
user_key_1 : true
}
}
users
- user_key_1{
tasks{
task_key_1 : true
}
}
Now, I would like to display a list of an user's tasks in an activity. I found this method and it's working:
myTasks.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
taskReference.child(dataSnapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
...
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, databaseError.toString());
}
});
}
public void onChildRemoved(DataSnapshot dataSnapshot) {
taskReference.child(dataSnapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot taskSnapshot) {
......
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, databaseError.toString());
}
});
}
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
taskReference.child(dataSnapshot.getKey()).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot taskSnapshot) {
Log.w(TAG, "on child changed")
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, databaseError.toString());
}
});
});
}
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.w(TAG, "onChildMoved");
}
#Override
public void onCancelled(DatabaseError error) {}
});
where:
mytasks is a reference to /users/userId/tasks
and taskReference is /tasks/taskKeyOwnedByUser.
My problem is: how can I detect changes to a task, i.e. when the user changes its title, and then update the list UI? It seems that the "Log.w" inside OnChildChanged never gets called.
Thank you in advance
This is my approach, change the value type of users/<usersId>/tasks/<taskId> to a timestamp value. Then when you are about to update the task, make sure to also update the timestamp so the onChildChanged will be triggered.
This is your database structure would be
users {
- user_key_1 {
tasks {
task_key_1 : ServerValue.TIMESTAMP (Long value)
}
}
}
And this is an example method when you are about to update the task's name
Map<String, Object> values = new HashMap<>();
values.put("tasks/" + task_key + "/name", new_task_name);
values.put("users/" + user_key + "/tasks/" + task_key, ServerValue.TIMESTAMP);
ref.updateChildren(values);
With this approach, the onChildChanged will be fired.
Hope this helps :)

Categories

Resources