John Spring
John Spring

Reputation: 49

Firebase data order

I have a class as seen Below:

public class GlobalHighScore {
String name;
int score;

public GlobalHighScore(String name, int score) {
    this.name = name;
    this.score = score;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getScore() {
    return score;
}

public void setScore(int score) {
    this.score = score;
}
}

And here I try to make a data receiving.

DatabaseReference scoresRef = firebaseDatabase.getReference("Highscores");    
scoresRef.child("GlobalHighScore").orderByChild("score").limitToFirst(10);
    scoresRef.addValueEventListener(new ValueEventListener() {
        @Override public void onDataChange(DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> keys = dataSnapshot.getChildren();
            int i = 0;
            for (DataSnapshot key : keys) {
                if(i == 10)
                    break;
                orderName[i].setText(key.getValue().toString());
                i++;
            }
        }

When I am doing this, key.getValue().toString() returns json formatted String but I want "name" and "score" seperately. Also, my data is not sorted eventhough I make it sorted.

scoresRef.child("GlobalHighScore").orderByChild("score").limitToFirst(10);

I think I having problem here.

Edit: When I order by "score", it gives data according to date.

Last form is

 final TextView[] orderScore = {firstscore, secondscore, thirdscore, fourthscore, fifthscore, sixthscore, seventhscore, eightscore, ninthscore, tenthscore};
    final TextView[] orderName = {firstname, secondname, thirdname, fourthname, fifthname, sixthname, seventhname, eightname, ninthname, tenthname};

    DatabaseReference scoresRef = firebaseDatabase.getReference("Highscores").child("GlobalHighScore");
    scoresRef.orderByChild("score").limitToFirst(10);
    scoresRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int i = 0;
            for (DataSnapshot data : dataSnapshot.getChildren()) {
                if(i == 10)
                    break;
                String name = data.child("name").getValue().toString();
                String score = data.child("score").getValue().toString();
                orderName[i].setText(name);
                orderScore[i].setText(score);
                i++;
            }
        }

It gives no data record at all.

Upvotes: 0

Views: 185

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80914

Try this:

 DatabaseReference scoresRef = firebaseDatabase.getReference("Highscores").child("GlobalHighScore");
Query q=scoresRef.orderByChild("score").limitToFirst(10);    
q.addValueEventListener(new ValueEventListener() {
    @Override public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot data : dataSnapshot.getChildren()){
          String name=datas.child("name").getValue().toString();
          String score=datas.child("score").getValue().toString();
        }
    }

Since you want the name and the score alone you can do the above, to be able to retrieve them alone.

.orderByChild("score").limitToFirst(10);, using this you will get the first 10, nodes that have the child score.

limitToFirst(10) //to get the first 10 of a specific child

limitToLast(10) //to get the Last 10 of a specific child

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138824

When you are using the following line of code:

Iterable<DataSnapshot> keys = dataSnapshot.getChildren();

The childrens that you are getting when iterating are actual objects, not Strings. Using toString() method doesn't make any sense because you cannot cast an Object to a String and expect to get the values within it. That's why you are getting that "json formatted Strings". So what are you getting when iterating are actual maps. So to get the name and score you need to iterate through the map and use: map.get("name"); and map.get("score");.

You don't need to change the entire code, your code is fine. Just solve this minor issue.

Upvotes: 0

Related Questions