0

I have a recyclerview that shows images from my Firebase Realtime Database. When I add new images/data to my Database, they show up at the very end of the recycleriew. What I want to do is simply reverse that order so that the newest data added to the database is at the beginning of the recyclerview and the oldest is all the way at the end.

I feel this would be a better experience for users as currently they have to scroll a long way before they see the latest images I've uploaded.

Here's the relevant Firebase code

dref = FirebaseDatabase.getInstance().reference
        natureList = ArrayList()


        ClearAll()
        GetDataFromFirebase()

@SuppressLint("NotifyDataSetChanged")
private fun GetDataFromFirebase(){

    val query: Query = dref!!.child("Space")

    query.addListenerForSingleValueEvent(object : ValueEventListener {
        @SuppressLint("NotifyDataSetChanged")
        override fun onDataChange(snapshot: DataSnapshot) {

            for (dataSnapshot: DataSnapshot in snapshot.children) {
                val nature = Nature()

                nature.space = dataSnapshot.child("space").value.toString()
                natureList.add(nature)
            }
            recyclerAdapterNature = NatureAdapter(requireActivity(), natureList, this@NatureWallpapers)

            natureRecyclerview.adapter = recyclerAdapterNature
            recyclerAdapterNature!!.notifyDataSetChanged()
        }

        override fun onCancelled(error: DatabaseError) {}
    })
    if (recyclerAdapterNature != null) recyclerAdapterNature!!.notifyDataSetChanged()
}

private fun ClearAll() {
    natureList.clear()
    natureList = ArrayList()
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Khumo Mashapa
  • 390
  • 1
  • 4
  • 13

0 Answers0