英文:
Generating a report in Firebase
问题
我有这个 Firebase 数据库:
它存储候选人,可以是总统、秘书或司库(类别)。
在投票期结束时,我想生成一个报告,报告中包含谁赢得了选举,即对于总统,检查具有最高“totalVotes”的候选人并获取他们的姓名。这将显示为赢得选举的候选人供选民查看。
这是否可行?我有以下代码:
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("总统最高分数为:" + highestpresident);
pres.setText(highestpresident);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // 不要吞掉错误
}
});
<!-- 结束片段 -->
英文:
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
}
});
<!-- end snippet -->
答案1
得分: 1
当然可以。最简单的解决方案是创建一个查询并按如下方式对结果进行排序:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference candidatesRef = rootRef.child("candidates");
Query totalVotesQuery = candidatesRef.orderByChild("totalVotes").limitToLast(1);
默认情况下,结果是按升序排列的。因此拥有最多选票数的候选人将始终位于最后。
如果您需要重新排序,请查看我在以下帖子中的回答:
编辑:
根据您的评论:
它给出的是键而不是候选人的姓名
它给您提供键,因为这是您请求的内容。如果您需要获取名字和姓氏的值,请使用以下代码:
String firstname = childSnapshot.child("firstname").getValue(String.class);
String lastname = childSnapshot.child("lastname").getValue(String.class);
pres.setText(firstname + " " + lastname);
英文:
> Is it possible to do this?
Sure, it is. The simplest solution for that would be to create a query and order the results like so:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference candidatesRef = rootRef.child("candidates");
Query tatalVotesQuery = candidatesRef.orderByChild("tatalVotes").limitToLast(1);
By default, the results are in ascending order. So the candidate with the highest number of votes will always be the last.
If you need to reorder them, please check my answer from the following post:
Edit:
According to your comment:
> It gives a key and not the name of the candidate
It gives you the key because this is what you request. If you need the value for the first name and last name, please use the following lines of code:
String firstname = childSnapshot.child("firstname").getValue(String.class);
String lastname = childSnapshot.child("lastname").getValue(String.class);
pres.setText(firstname + " " + lastname);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论