I've been trying to find a way to query by the child node 'votes' to order the data by number of 'votes'. I'm pretty sure because of the way I have stored the data in firebase it cant be queried but I currently have to store it this way.
{
"45" : {
"Biological and Ecological" : {
"Votes" : 1
}
},
"567" : {
"Technology" : {
"Votes" : 2
}
},
"620" : {
"Technology" : {
"Votes" : 1
}
},
"702" : {
"Social and Behavioral" : {
"Votes" : 1
}
}
}
I am attempting to order by the child "Votes" is there a way to do this? Below is the method in which it is currently displayed I am aware that .OrderbyChild("Votes") isnt defining the path correctly but is there a way to achieve this?
Query query = FirebaseDatabase.getInstance().getReference("CountLikes/").orderByChild("Votes");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot child: dataSnapshot.getChildren()) {
for(DataSnapshot grandchild: child.getChildren()) {
arrayList.add("Project " + child.getKey() + " " + grandchild.getKey() + " " + (String.valueOf(grandchild.child("Votes").getValue(Long.class))));
adapter.notifyDataSetChanged();
}
}}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});