firebase检索最高100分 [英] firebase retrieve highest 100 score

查看:144
本文介绍了firebase检索最高100分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的firebase的屏幕截图

i试图检索Firebase数据库中最高的100分
i使用此代码向firebase添加新节点

  Map< String,String> post1 = new HashMap< String,String>(); 
post1.put(name,name);
post1.put(score,score);
myRef.push()。setValue(post1);

这是我用来检索最高100分的代码,代码的作品,但它不是检索最高的100分)

 查询queryRef = myRef.orderByChild(score)。limitToFirst 100); (DataSnapshot postSnapshot:dataSnapshot.getChildren())返回一个新的DataEventListener对象,并将其添加到DataSnapshot的DataSnapshot对象中。 {
Score score = postSnapshot.getValue(Score.class);
Log.d(test,values is+ score.getName()++ score.getScore());


$ b @Override
public void onCancelled(DatabaseError databaseError){


});


解决方案

Firebase查询始终以升序排列。因此,您需要获取 last 100,而不是前100个。

 查询queryRef = myRef.orderByChild(score)。limitToLast(100); 

然后在客户端,您需要将这些项目反向。



另外,你可以添加一个倒置的属性到你的项目 invertedScore:-99 。如果你这样做的话,你可以按倒数得分来排序,而不必颠倒数组。



这个场景已经被经常覆盖了。我强烈建议你研究一下这些:

>

this is a screen shot of my firebase i am trying to retrieve the highest 100 score in firebase database i am using this code to add new node to firebase

 Map<String, String> post1 = new HashMap<String, String>();
        post1.put("name",name);
        post1.put("score",score);
        myRef.push().setValue(post1);

and this is the code i am using to retrieve the highest 100 score which doesn't work (the code works but it is not retrieving the highest 100 score)

 Query queryRef = myRef.orderByChild("score").limitToFirst(100);
        queryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                    Score score=postSnapshot.getValue(Score.class);
                    Log.d("test"," values is " + score.getName()  + " " + score.getScore());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        }); 

解决方案

Firebase queries are always in ascending order. So you'll need to get the last 100, instead of the first 100.

Query queryRef = myRef.orderByChild("score").limitToLast(100);

Then client-side you'll need to reverse the items.

Alternatively you can add a inverted property to your items invertedScore: -99. If you do that, you can order by that inverted score and won't have to reverse the array.

This scenario has been covered frequently before. I highly recommend you study some of these:

这篇关于firebase检索最高100分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆