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) {
}
});
Related
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.
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) {
}
});
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
I have a ChildEventListener to iterate through a list of children in a Firebase node. Each of those children is a child of another node, and I am trying to access them using a ValueEventListener inside of my ChildEventListener. The ValueEventListener won't call at all. I have a print statement in the onDataChange method and it doesn't print.
databaseReference.addOnChildEventListener(new ChildEventListener(){
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("PRINT");
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#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) {
}
});
Theres more to databaseReference before I call the addOnChild... or addListener... but I don't know why it won't print the statement.
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