当我从 firebase 实时数据库获取数据时,它会自动按升序列出,但我试图让我放入 arraylist 的值以相反的顺序而不是升序列出。
使用 listview 时是否可以对值进行相反的排序?
目前使用的代码是:
Query query = userRef.orderByChild("totalNumberOfSteps");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String username = ds.child("username").getValue(String.class);
long totalNumberOfSteps = ds.child("totalNumberOfSteps").getValue(Long.class);
Log.d(TAG , username + " - Steps: " + totalNumberOfSteps);
arrayList.add(new String(username + " - " + "steps: " + totalNumberOfSteps));
arrayAdapter = new ArrayAdapter<>(Leaderboard.this , android.R.layout.simple_list_item_1 , arrayList);
listView.setAdapter(arrayAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG , databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);}}
此代码的当前输出是:
John Doe VI - Steps: 0
John Doe III - Steps: 0
John Doe V - Steps: 12
John Doe I - Steps: 79
John Doe IV - Steps: 88
John Doe II - Steps: 111
首选输出:
John Doe II - Steps: 111
John Doe IV - Steps: 88
John Doe I - Steps: 79
John Doe V - Steps: 12
John Doe III - Steps: 0
John Doe VI - Steps: 0
添加Collections.reverse(arrayList);到代码时,输出为:
John Doe II - Steps: 111
John Doe IV - Steps: 88
John Doe III - Steps: 0
John Doe VI - Steps: 0
John Doe V - Steps: 12
John Doe I - Steps: 79
肥皂起泡泡
相关分类