【发布时间】:2016-11-13 06:45:52
【问题描述】:
在MainActivity里面,我有这个功能:
public void sortingData() {
mRootRef = new Firebase("https://fyp-1-43a27.firebaseio.com/User");
dbSortAllData = FirebaseDatabase.getInstance().getReference();
ListView mLvScore = (ListView) findViewById(R.id.lvScore);
final List<UserInformation> listScore = new LinkedList<>();
final ArrayAdapter<UserInformation> adapter
= new ArrayAdapter<UserInformation>
(this, R.layout.scoreboard_row, listScore) {
@NonNull
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = getLayoutInflater().inflate(R.layout.scoreboard_row, parent, false);
}
UserInformation item = listScore.get(position);
((TextView) view.findViewById(R.id.tvScoreName)).setText(item.getName());
((TextView) view.findViewById(R.id.tvScorePoint)).setText(item.getScore());
return view;
}
};
mLvScore.setAdapter(adapter);
com.firebase.client.Query queryRef = mRootRef.orderByChild("score").startAt("score").endAt("score").limitToFirst(50);
queryRef.addChildEventListener(new com.firebase.client.ChildEventListener() {
@Override
public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
UserInformation sI = dataSnapshot.getValue(UserInformation.class);
listScore.add(sI);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(com.firebase.client.DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(com.firebase.client.DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(com.firebase.client.DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
我的模型课:
public class UserInformation implements Comparable<UserInformation> {
public String name;
public String age;
public String score;
public UserInformation() {}
public UserInformation(String name, String age, String score) {
this.name = name;
this.age = age;
this.score = score;
}
// ... Getters and Setters here.
}
这些代码有效,从最低到最高排序。
我的问题是如何从最高到最低排序?非常感谢您的建议和建议。
【问题讨论】:
-
Firebase 按字典顺序排序,AFAIK。您可以反转 ArrayAdapter 的数据
-
你为什么使用 firebase.com 而不是 firebase.google.com ? firebase.com 已弃用。
-
Collections.sort(scores, new Comparator
() { public int compare(Score o1, Score o2) { return o2.getScores().get(0).compareTo(o1.getScores ().get(0)); } });上面的代码是我在互联网上找到的,但我真的不知道如何适应它。任何指导表示赞赏
标签: android sorting firebase firebase-realtime-database