'Query inside a query returning not what I want FireBase, android studio
I have a problem, I would like to populate my FirebaseRecyclerOptions
with a query inside another query so I used a method from this question : How to perform join query in Firebase?.
Query query = FirebaseDatabase.getInstance().getReference()
.child("Favoris")
.orderByChild("idUser")
.equalTo(user.getUid());
query.addValueEventListener(
new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot child : snapshot.getChildren()){
Map<String, Object> valuesMap = (HashMap<String, Object>) child.getValue();
String key = (String) valuesMap.get("idAnnonce");
Query query2 = FirebaseDatabase.getInstance().getReference()
.child("Annonces")
.orderByChild("id")
.equalTo(key);
query2.addValueEventListener(
new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot child: snapshot.getChildren()){
Annonce annonce = child.getValue(Annonce.class);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
}
);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.d("errror", error.getDetails());
}
}
);
query.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (!task.isSuccessful()) {
Log.e("firebase", "Error getting data", task.getException());
}
else {
Log.d("firebase", String.valueOf(task.getResult().getValue()));
}
}
});
But when I exec my code, the query.get() return object "Favori" from the first query and It's not what I want. I would like to have the Annonce object. I hope someone could help me to resolve my problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|