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) {
}
});
Related
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) {
}
});
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.
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
The first 2 keys dont have child but others have them. I'm able to display the child elements when there is only one parent and I can display Admin/User details using data object model but how to display parent elements(Bright Kid Test & others) in Listview?
This is an image of Firebase Database:
Code:
listView = (ListView)findViewById(R.id.preschool_list);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,list);
listView.setAdapter(adapter);
databaseReference = FirebaseDatabase.getInstance().getReference("Preschools");
databaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
list.add(value);
adapter.notifyDataSetChanged();
}
#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) {
}
});
If there is only one child of preschools then it is displayed else no value is shown.
I've got the answer with datasnapshot.getChildren().
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dsp : dataSnapshot.getChildren())
{
list.add(String.valueOf(dsp.getKey()));
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
You want to display text Bright Kid LL, Bright Kid Mont, Bright Kid Test in list view, don't you? You can get them by getKey()
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot recipeSnapshot : dataSnapshot.getChildren()) {
String key = recipeSnapshot.getKey();
list.add(key);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
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.