我使用的是Firebase数据库。我的用户"数据看起来像这样:
Users {
"38NFjDwCPAdkdvbzLb1VkFeaBIV2" : {
"points" : 10,
"userEmail" : "abc@hotmail.com",
"userName" : "Tom"
},
"pH3HWzu9SVbW56jwqCb55hHryAD2" : {
"points" : 17,
"userEmail" : "xyz@hotmail.com",
"userName" : "Jerry"
}
}
现在我想按降序显示用户的点数。我可以使用orderByChild("points")
,但只能按升序显示列表。
这就是我现在正在做的事情:
public class Leaderboards extends AppCompatActivity {
private ListView leaderboardsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboards);
leaderboardsList = (ListView) findViewById(R.id.leaderboardList);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("Users");
DatabaseReference carsRef = rootRef.child("Card");
Query qUserRef = userRef.orderByChild("points");
FirebaseListAdapter<User_DB> firebaseListAdapter = new FirebaseListAdapter<User_DB>(this, User_DB.class, android.R.layout.simple_list_item_1, qUserRef ) {
@Override
protected void populateView(View v, User_DB model, int position) {
String points = String.valueOf(model.getPoints());
((TextView)v.findViewById(android.R.id.text1)).setText(points);
}
};
leaderboardsList.setAdapter(firebaseListAdapter);
}
}
根据每个用户的观点,我对上面的代码做了哪些更改才能按降序显示我的列表?
答案 0 :(得分:-1)
您需要为每个数据设置优先级并使用orderByPriority
功能
因为在Firebase上,我们无法设置降序或升序到排序。