Retrieving data in alphabetical order using Firebase in Android Studio [duplicate]

半腔热情 提交于 2020-01-24 00:31:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!