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);
这是我用来检索最高 100 分的代码,但它不起作用(代码可以工作,但它没有检索最高 100 分)
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 查询始终按升序排列。所以你需要得到last100,而不是前 100。
Query queryRef = myRef.orderByChild("score").limitToLast(100);
然后在客户端您需要反转这些项目。
或者,您可以向您的项目添加反向属性invertedScore: -99
。如果这样做,您可以按反转的分数进行排序,而不必反转数组。
这种情况之前已经被频繁报道过。我强烈建议您学习其中的一些内容:
- firebase中的数据排序 https://stackoverflow.com/questions/19278116/data-sorting-in-firebase/19278582#19278582
-
Firebase 排行榜,最佳实施。 SQL 连接和 orderby https://stackoverflow.com/questions/39449507/firebase-leaderboard-best-implmentation-sql-joins-and-orderby/39453272#39453272(就在昨天)
- 按发布降序显示帖子 https://stackoverflow.com/questions/25611356/display-posts-in-descending-posted-order/25613337#25613337
- Android 中的 Firebase 数据描述排序 https://stackoverflow.com/questions/34156996/firebase-data-desc-sorting-in-android/34158197#34158197
- Swift - 如何在 Firebase 上创建降序排序查询? https://stackoverflow.com/questions/36314374/swift-how-to-create-sort-query-as-descending-on-firebase
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)