Let's say I have some data as follow:
"teilnehmer" : {
"ATYFdfNRThzrEC83" : {
"score" : 2500,
"teamname" : "Was ist mit Karsten los?"
},
"VMT9hp65Tui55J2" : {
"score" : 12500,
"teamname" : "Team Zombie.de"
},
"dPkdf233dbMjCA2" : {
"score" : 21635,
"teamname" : "Bielefeld zwei"
},
"U1xTtKJa0wdZ13" : {
"score" : 500,
"teamname" : "Max der Eugen"
}.............
And this Java Code:
mDatabase.child("rallye").child("teilnehmer").orderByChild("score").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// teamname, Platzierung, Punkte
Teilnehmer teilnehmer = new Teilnehmer(((String) dataSnapshot.child("teamname").getValue()), "Platz x", (dataSnapshot.child("score").getValue())+" Punkte");
teilnehmerList.add(teilnehmer); // add to recyclerview
mAdapter.notifyDataSetChanged(); // save
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
//adapter.remove((String) dataSnapshot.child("teamname").getValue());
//teilnehmerList.remove();
mAdapter.notifyDataSetChanged();
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
@Override
public void onCancelled(DatabaseError databaseError) {}
});
This doesn't work
.orderByChild("score")
Screenshot here : i.stack.imgur.com/Y8NlW.png
I get the teamname and the teamscore in a Rallygame. But the order is ascending, not descending :/
Output:
Teamname ----------- Score
Team Zombie 50
Team xyz 1500
Team foo 16000
Wrong order :/
And then I have a second question, how can I output the current Position in Ranking?
For example:
My List show something like this:
Rank Teamname Points
1 "Team 1" 25.000
2 "Team red" 12.000
3 "Team google" 9.500
4 "Team yellow" 1.250
My Firebase contain:
"ATYFdfNRThzrEC83" : {
"score" : 2500,
"teamname" : "Was ist mit Karsten los?"
},
I have only 2 columns, score and teamname
In my first Question i solve the problem with the Order (most Points -> first place)
My layout file:
<TextView
android:id="@+id/teamName"
android:textColor="@color/teamTitle"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/teamRank"
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/teamScore"
android:textColor="@color/teamScore"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
3 Output TextViews, but only 2 Datacolumns in Firebase. The Team with the Most points will be on Rank / Place 1 (i dont know the correct english word for my German "Platz n"). Than Rank 2, Rank 3....
My first Try:
int i = 1;
while(dataSnapshot.child("teamname").getValue()) {
Teilnehmer teilnehmer = new Teilnehmer(((String) dataSnapshot.child("teamname").getValue()), "Platz "+i, (dataSnapshot.child("score").getValue())+" Punkte");
teilnehmerList.add(0, teilnehmer); // add to recyclerview: add(0, teilnehmer) gebe in desc aus von 0 zum n. eintrag
mAdapter.notifyDataSetChanged(); // save
i++;
}
I would like three ouputs in one row. Teamname, Rank in Ranking and the current score in the Game.
but my while Loop does not work.
In this function:
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// teamname, Platzierung, Punkte
Teilnehmer teilnehmer = new Teilnehmer(((String) dataSnapshot.child("teamname").getValue()), "Platz x", (dataSnapshot.child("score").getValue())+" Punkte");
teilnehmerList.add(teilnehmer); // add to recyclerview
mAdapter.notifyDataSetChanged(); // save
}
I assume you are adding to an array list. Instead of adding to the end, add to the beginning because orderByChild
is working, its in ascending order. Firebase does not support descending order.
teilnehmerList.add(0, teilnehmer); // <--- add 0 as 1st parameter
Your second question is lacking detail. Create another question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.