2

I'm developing an app in which I'm retrieving some data from Firebase database.

The problem is that everytime I save something, it gets retrieved and gets added to the bottom of the RecyclerView.

What I want is I want the newly added data to get added to the top of the recyclerview and not at the bottom.

I have no idea how to do what I want to do.

Please let me know.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133

2 Answers2

5

well first of all the question is about recyclerview and you posted code of your data retrieval. and if your want to add a new item to recycler view use

mArrayList.add(position, item);
notifyItemInserted(position); 

for data to be inserted in first row use

mArrayList.add(0, item);
notifyItemInserted(0); 

and then you can use mRecyclerView.smoothScrollToPosition(0); to scroll to the top.

M.Waqas Pervez
  • 2,492
  • 2
  • 19
  • 33
  • awesome. this did the job! can you please have a look at this too: http://stackoverflow.com/questions/38023511/how-to-place-ads-randomly-in-between-of-recyclerview-please-see-details Please?... – Hammad Nasir Jun 25 '16 at 06:43
3

try something like

Query lastToFirstQuery = databaseReference.orderBy("something_in_your_database").limitToLast(datasnapshot.numChildren());

lastToFirstQuery.addChildEventListener(new ChildEventListener() { 
        @Override 
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Map<String, String> newRequest = (Map<String, String>) dataSnapshot.getValue();
            imageUID = newRequest.get("imageUIDh");
            hDescription = newRequest.get("hDescription");
            currentLat = newRequest.get("currentLat");
            currentLng = newRequest.get("currentLng");
            postedBy = newRequest.get("postedBy");
            postedAtTime = newRequest.get("postedAtTime");
            postedOnDate = newRequest.get("postedOnDate");
            utcFormatDateTime = newRequest.get("utcFormatDateTime");

        } 

        @Override 
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        } 

        @Override 
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        } 

        @Override 
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) {
            Snackbar snackbar = Snackbar
                    .make(coordinatorLayout, databaseError.getMessage(), Snackbar.LENGTH_LONG);
            snackbar.setDuration(Snackbar.LENGTH_SHORT);
            snackbar.show();
            progressBarLoading.setVisibility(View.INVISIBLE);
        } 
    }); 
Siddhesh Dighe
  • 2,894
  • 3
  • 16
  • 16
  • M. Waqas answer solved the issue. Thanks for your answer too. Can you please have a look at this too: stackoverflow.com/questions/38023511/… Please?.. – Hammad Nasir Jun 25 '16 at 06:43