问题
This question is a follow up question from a previous post.
Is there anyway to order the results alphabetically? I'm looking for something like Query's orderByValue().
...
protected ArrayList<String> usernames = new ArrayList<>();
Firebase userRef = ref.child("users");
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
usernames.clear(); // Clear list before updating
for (DataSnapshot child : dataSnapshot.getChildren()) {
String username = (String) child.child("username").getValue();
usernames.add(username);
Log.v(TAG, usernames + "");
}
...
回答1:
I believe you want Collections.sort(usernames) once you've read all of the children from the dataSnapshot.
How can I sort an ArrayList of Strings in Java?
来源:https://stackoverflow.com/questions/32530957/retrieving-data-in-alphabetical-order-using-firebase-in-android-studio