【发布时间】:2017-06-19 21:27:28
【问题描述】:
我有一个关于我的代码的简短问题。
我在我的 Android 应用程序中使用了一个 recyclerview,并用我的 Firebase 数据库中的一些数据填充它。
效果很好,但我注意到排序不正确:/
我有这个代码:
private void getRankings() {
// hole anzahl an Teilnehmern
mDatabase.child(TEILNEHMERDATA).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// hole Anzahl aller Teilnehmer um das Ranking zu bestimmen
teilnehmerVar = (int) dataSnapshot.getChildrenCount();
mDatabase.child(TEILNEHMERDATA).orderByChild("score").addChildEventListener(new ChildEventListener() {
Teilnehmer teilnehmer = new Teilnehmer();
int i = teilnehmerVar+1;
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// teamname, Platzierung, Punkte
int punkteStand = (int) Double.parseDouble(String.valueOf(dataSnapshot.child("score").getValue()));
i -= 1;
this.teilnehmer = new Teilnehmer(dataSnapshot.getKey(), ((String) dataSnapshot.child("teamname").getValue()), "Platz " + i, punkteStand + " Punkte");
teilnehmerList.add(0, this.teilnehmer); // add to recyclerview: add(0, teilnehmer) gebe in desc aus von 0 zum n. eintrag
mAdapter.notifyDataSetChanged(); // save
}
在这一行:
mDatabase.child(TEILNEHMERDATA).orderByChild("score").addChildEventListener(new ChildEventListener() {
按分数排序孩子
我的数据结构如下:
"Teilnehmer" : {
"user_0gPpHXzKqLd2xA" : {
"image" : "",
"score" : "0",
"startnummer" : "1",
"teamname" : "Andy Foo"
},
行分数包含一个整数,其数据范围从 0 到 ∞
我的布局如下:
Teamname1 9 Punkte
Platz 1
Teamname2 0 Punkte
Platz 2
Teamname3 1000 Punkte
Platz 3
但这绝对是错误的:/
9 大于 10000 似乎我的查询顺序只有第一个数字
1..2..3..
我的 Recyclerview 初始化:
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
谁能帮帮我?
【问题讨论】:
-
你试过用
orderByValue()方法吗? -
这是一个快速的回复。是的,尝试使用 orderByValue().startAt("0").endAt("99999999") 但不起作用(我没有得到任何数据)
-
你有没有试过用这行代码代替:
mDatabase.child(TEILNEHMERDATA).orderByChild("score").addChildEventListener(new ChildEventListener() {这行代码:mDatabase.child(TEILNEHMERDATA).child(userId).orderByChild("score").addChildEventListener(new ChildEventListener() {?其中userId是在TEILNEHMERDATA节点正下方看到的ID。 -
谢谢,它适用于整数或双数。但是我必须使用后端(对于其他人)来管理这些数据,并且这些后端将所有数据元素存储为字符串...我可以使用规则验证将这些字符串转换为整数或双精度值吗?这也可以解决我的问题。我无法更改后端(来自 3rd 方开发人员的 Web Gui)以将这些元素直接存储为 int / double
-
@AlexMamo TEILNEHMERDATA 仅包含表名:private String TEILNEHMERDATA = "Teilnehmer"; userID 是 firebase 身份验证 UID + 我的前缀 user_$UID 我将尝试使用 .child(user_$iuid)
标签: android firebase firebase-realtime-database android-recyclerview