0

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();
  • If you are using FirebaseUI then [@Alex Mamo's][1] answer is best else you can create a new list with filling items in reverse order from you original list. then send it to adapter. – Nishant Bhakta Oct 16 '17 at 19:04

2 Answers2

0

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)
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • No I am not using firebase ui... Actually my main activity have a linear layout which is the parent of custom notice layout.... – Shadab Azam Farooqui Oct 16 '17 at 19:01
  • In this case, add all the elements from your `snapshotIterator` to a list and use then `reverseOrder(Comparator cmp)` according to the offical [doc](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html) of Collections and in the end pass it to the adapter. – Alex Mamo Oct 16 '17 at 19:04
  • Should work. What is the error? Have you declared and used the list inside the `onDataChange()` method? Because this how you need to do. – Alex Mamo Oct 16 '17 at 20:06
  • data is not being interted in list completely. – Shadab Azam Farooqui Oct 16 '17 at 20:15
  • I don't understand you. Does it work if you declare and use the list inside `onDataChange()` method? – Alex Mamo Oct 16 '17 at 20:17
  • Sir this is a callback and that's why below line of this callback is being execute before the inserting all record... And hence some time no record and some time only single record is being shown... Got it sir? – Shadab Azam Farooqui Oct 17 '17 at 03:47
  • Yes and that's why i suggested you to declare and use the list only inside the `onDataChange()` method. Have you tried this approach? – Alex Mamo Oct 17 '17 at 08:29
  • Happy to hear that. If you think that my answer helped you, please consider accepting it. Thanks! – Alex Mamo Oct 20 '17 at 10:33
  • Yes of course and but what have to done? Means How i can consider it as an answer, by using new comment or something else? – Shadab Azam Farooqui Oct 20 '17 at 20:54
0

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