Skip to content Skip to sidebar Skip to footer

Make A Top Ten Of The Best Players Using Firebase Database

I am new in Firebase Database and I'm struggling with this problem. What I want to achieve is: show a top ten of the best players based on their 'hits'. I know I could use firebase

Solution 1:

I think you missing two things:

  1. a limitToLast(10) so that you get the 10 highest scores only
  2. use .orderByChild("easy/hits"), since that's where the value is stored

So:

Querymquery= FirebaseDatabase.getInstance().getReference()
                        .child("player")
                        .orderByChild("easy/hits")
                        .limitToLast(10);

Note that a Map is inherently unordered, so not a good structure to keep an ordered list in. You'll want to use some List structure for that.

publicvoidonDataChange(DataSnapshot dataSnapshot) {
    List<String> names = newLinkedList<String>();
    for (DataSnapshotpostSnapshot: dataSnapshot.getChildren()) {
        String key = postSnapshot.getKey();
        String name = postSnapshot.child("easy/name").getValue(String.class);
        names.add(0, name);
    }
}

You'll note that I call names.add(0, name). This ensures that the results end up in the list in descending order, like you'd expect on a leaderboard.

Post a Comment for "Make A Top Ten Of The Best Players Using Firebase Database"