I would like to know how can I retrieve the key of this object based on the object content. I'm using a realtime firebase database.
JSON code :
"Places" : {
"-M11-OIIHbKTVqCJ-Swv" : {
"name" : "Farmacia Pagani Dr. Roberto",
"others" : {
"Abilify" : 100,
"Acetaminophen" : 120,
"Acyclovir" : 140,
"Adderall" : 160
}
},
"-M11-OJJCGAPdJUknLXX" : {
"name" : "FARMACIA DR. TUMIATTI MARIANO",
"others" : {
"Abilify" : 100,
"Acetaminophen" : 120,
"Acyclovir" : 140,
"Adderall" : 160
}
}
I want to retrieve M11-OIIHbKTVqCJ-Swv if I have the name : Farmacia Pagani Dr. Roberto"
The code I wrote :
mUserDatabase = FirebaseDatabase.getInstance().getReference("Places");
Query query = mUserDatabase.orderByChild("name").equalTo(nameOfThePharmacy);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("The key is : "+dataSnapshot.getKey());
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
This code doesn't work, it returns : "Places" but I want the key of the object that contains the field name that equals with = ad example Farmacia Pagani Dr. Roberto , the output should be : -M11-OIIHbKTVqCJ-Swv