我有代码:
listView = (ListView) findViewById(R.id.list);
list = new ArrayList<String>();
arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
leaderBoard();
}
FirebaseAuth auth = FirebaseAuth.getInstance();
public void leaderBoard(){
database.getReference().child("db/auth/user/").addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
list = new ArrayList<String>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
System.out.println("The score is: " + snapshot.child("score").getValue());
String s = snapshot.child("name").getValue() + " " + snapshot.child("score").getValue();
list.add(s);
arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
我的目标是显示一个列表,其中包含用户的名称和得分,到目前为止,效果很好,如照片所示。
但是现在我需要该列表,以便根据得分以递增顺序显示用户和得分。有人可以帮我吗?
答案 0 :(得分:1)
要按子项score
属性的值对子项进行排序,可以执行以下操作:
database.getReference().child("db/auth/user/").orderByChild("score").addValueEventListener(new ValueEventListener() {
...
据我所知,其余代码可以保持不变。
另请参阅ordering and filtering data上的Firebase文档。