[英]How can I get firebase items in descending order in Java / Android
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: 这段Java代码:
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.stack.imgur.com/Y8NlW.png
I get the teamname and the teamscore in a Rallygame. 我在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: 我的Firebase包含:
"ATYFdfNRThzrEC83" : {
"score" : 2500,
"teamname" : "Was ist mit Karsten los?"
},
I have only 2 columns, score and teamname 我只有2列,分数和队名
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. 3个输出TextView,但Firebase中只有2个数据列。 The Team with the Most points will be on Rank / Place 1 (i dont know the correct english word for my German "Platz n").
得分最高的团队将排在排名第1位(我不知道我的德语“ Platz n”的正确英语单词)。 Than Rank 2, Rank 3....
比等级2,等级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. 但是我的while循环不起作用。
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. 而不是添加到末尾,而是添加到开头,因为
orderByChild
正在工作,并且按升序排列。 Firebase does not support descending order. Firebase不支持降序。
teilnehmerList.add(0, teilnehmer); // <--- add 0 as 1st parameter
Your second question is lacking detail. 您的第二个问题缺少细节。 Create another question.
创建另一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.