在Firebase中生成报告。

huangapple go评论81阅读模式
英文:

Generating a report in Firebase

问题

我有这个 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:

在Firebase中生成报告。

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(&quot;candidates&quot;);
        Query tatalVotesQuery = candidatesRef.orderByChild(&quot;tatalVotes&quot;).limitToLast(1);
        tatalVotesQuery.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot){
                for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                    highestpresident = childSnapshot.getKey();

                    System.out.println(&quot;President highest score is: &quot; + highestpresident);
                    pres.setText(highestpresident);
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException(); // don&#39;t swallow errors
            }
        });

<!-- end snippet -->

答案1

得分: 1

当然可以。最简单的解决方案是创建一个查询并按如下方式对结果进行排序:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference candidatesRef = rootRef.child("candidates");
Query totalVotesQuery = candidatesRef.orderByChild("totalVotes").limitToLast(1);

默认情况下,结果是按升序排列的。因此拥有最多选票数的候选人将始终位于最后。

如果您需要重新排序,请查看我在以下帖子中的回答:

https://stackoverflow.com/questions/47721971/how-to-arrange-firebase-database-data-in-ascending-or-descending-order

编辑:

根据您的评论:

它给出的是键而不是候选人的姓名

它给您提供键,因为这是您请求的内容。如果您需要获取名字和姓氏的值,请使用以下代码:

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(&quot;candidates&quot;);
Query tatalVotesQuery = candidatesRef.orderByChild(&quot;tatalVotes&quot;).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:

> https://stackoverflow.com/questions/47721971/how-to-arrange-firebase-database-data-in-ascending-or-descending-order

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(&quot;firstname&quot;).getValue(String.class);
String lastname = childSnapshot.child(&quot;lastname&quot;).getValue(String.class);
pres.setText(firstname + &quot; &quot; + lastname);

huangapple
  • 本文由 发表于 2020年4月8日 20:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定