I'm trying to sort the restaurants by the average rating with orderByChild() but instead of returning them by sorting them by average rating it doesn't.
Here is my code:
public void filterQuality(){
DatabaseReference mRatingDb = FirebaseDatabase.getInstance().getReference();
Query topQualityQuery = mRatingDb.orderByChild("Average");
topQualityQuery.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Map <String, Double> topQualityMap = new HashMap<>();
List list = new ArrayList();
List list2 = new ArrayList();
for (DataSnapshot ds: dataSnapshot.getChildren()){
topQualityMap.put(ds.getKey(), ds.child("Quality").child("Average").getValue(double.class));
list.add(ds.child("Quality").child("Average").getValue(double.class));
list2.add(ds.getKey());
Log.d("Top Quality", "Value" + topQualityMap);
Log.d("Top Quality", "Value" + list);
Log.d("Top Quality", "Value" + list2);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
This is what I get from maps and lists from what I tried to debug:
Value [3.0]
Value [Jurys Inn Newcastle]
Value {Malmaison Newcastle=2.0, Jurys Inn Newcastle=3.0}
D/Top Quality: Value [3.0, 2.0]
D/Top Quality: Value [Jurys Inn Newcastle, Malmaison Newcastle]
D/Top Quality: Value {Malmaison Newcastle=2.0, Jurys Inn Newcastle=3.0, The County Hotel, Newcastle=4.0}
Value [3.0, 2.0, 4.0]
Value [Jurys Inn Newcastle, Malmaison Newcastle, The County Hotel, Newcastle]
Here is my firebase:
If someone knows why this is not working properly please help. Thank you.
You'll need to specify the full path to the value you want to order on.
So:
DatabaseReference mRatingDb = FirebaseDatabase.getInstance().getReference();
Query topQualityQuery = mRatingDb.orderByChild("Quality/Average");
topQualityQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
Log.i("Firebase", ds.getKey()+": "+ ds.child("Quality/Average").getValue(double.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // NEVER ignore errors
}
});