Here is my database:
For each category, it is supposed to return the candidate with the highest 'totalvotes'.
Here is my code:
Query presidentquery = reference.child("candidates").orderByChild("category").equalTo("President");
presidentquery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String firstname = dataSnapshot1.child("firstname").getValue(String.class);
String lastname = dataSnapshot1.child("lastname").getValue(String.class);
Long tvotes = dataSnapshot1.child("totalVotes").getValue(Long.class);
pres.setText(firstname + " " + lastname+" - "+tvotes+" Votes");
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Instead of returning the candidate with highest votes, it returns the most recent candidate. How can I filter the results in datasnapshot to display the candidate with highest 'totalVotes'.
Thanks