How can i print data in reverse order using firebase in android

旧时模样 提交于 2019-12-31 05:12:45

问题


i have a noticeboard activity to show to the notice. Now i want to print last notice at first position. Means in reverse order.

            if (dataSnapshot.getValue() != null) {
                Iterable<DataSnapshot> snapshotIterator = dataSnapshot.getChildren();
                Iterator<DataSnapshot> iterator = snapshotIterator.iterator();

                while (iterator.hasNext()) {
                    //notificationCount++;
                    DataSnapshot snapshot = iterator.next();

回答1:


If you are using FirebaseUI please use the following lines of code:

LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager)



回答2:


This is the answer.

In this case, add all the elements from your snapshotIterator to a list and use then reverseOrder(Comparator cmp) according to the offical doc of Collections and in the end pass it to the adapter. thanks to Mr. Alex Mamo



来源:https://stackoverflow.com/questions/46777129/how-can-i-print-data-in-reverse-order-using-firebase-in-android

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