I have this firebase database:
It stores candidates, who can be either president, secretary or treasurer (categories).
At the end of a voting period, I want to generate a report with who won, i.e, for President, check the candidate with the highest 'totalVotes' and get their name. it will be displayed as the winning candidate to the voters. Is it possible to do this? I have this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference candidatesRef = rootRef.child("candidates");
Query tatalVotesQuery = candidatesRef.orderByChild("tatalVotes").limitToLast(1);
tatalVotesQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
highestpresident = childSnapshot.getKey();
System.out.println("President highest score is: " + highestpresident);
pres.setText(highestpresident);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't swallow errors
}
});