0

So this is my GridLayout I have declared LinearLayoutManager.VERTICAL, true

      var recyclerViewUploaded: RecyclerView? = null
        recyclerViewUploaded = my_posts_recycle
        recyclerViewUploaded.setHasFixedSize(true)
        val gridLayoutManager = GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, true )
        recyclerViewUploaded.layoutManager = gridLayoutManager
        postList = ArrayList()
        postAdapter = MypostsAdapter(this, postList as ArrayList<Post>)
        recyclerViewUploaded.adapter = postAdapter

App Screenshot

See the above image, the problem is it leaves a blank space at the top instead of the bottom.

I have tried using Collections.reverse(postList) but it gives unexpected results the post order 123456 becomes 125643 instead of 654321.

Also postList.reverse() dosen't do anything!

Here is my function to retrive images.

  private fun retrievePosts() {
   val postsRef = FirebaseDatabase.getInstance().reference.child("Posts")
    postsRef.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            if (dataSnapshot.exists()) {
                (postList as ArrayList<Post>).clear()
                for (snapshot in dataSnapshot.children) {
                    val post = snapshot.getValue(Post::class.java)
                    if (post!!.getPublisher() == profileId) {
                        (postList as ArrayList<Post>).add(post)
                    }
                    if (postList.isNullOrEmpty()){
                        swipe_refresh_profile.isRefreshing = false
                    }
                    postList!!.reversed()
                    swipe_refresh_profile.isRefreshing = false
                    postAdapter!!.notifyDataSetChanged()
                }
            }
        }

        override fun onCancelled(error: DatabaseError) {
            TODO("Not yet implemented")
        }
    }
    )
}

Here is my JSON database structure

 "Posts" : {
"-MYkL70voeqXhrkli7vE" : {
  "caption" : "Setup by:- @iamlokesh_04\n\nSector Kwgt\nhttps://play.google.com/store/apps/details?id=sector.kustom.pack\n\nMini-music Kwgt\nhttps://play.google.com/store/apps/details?id=minimusicplayerforkwgt.kustom.pack\n\nSelf-made icons\nhttps://t.me/Iamlokesh_04\n\nHope you guys like it  ",
  "postid" : "-MYkL70voeqXhrkli7vE",
  "postimage" : "https://firebasestorage.googleapis.com/v0/b/android-customization.appspot.com/o/Posted%20Pictures%2FAkZI2A4EZbMOHCHPqZp4h4hl2zL2%2F1618940031310.jpg?alt=media&token=946e7836-b032-4d95-93ac-1c04d4342161",
  "publisher" : "AkZI2A4EZbMOHCHPqZp4h4hl2zL2"
},
"-MYkLhERZIZ5az-K3VrQ" : {
  "caption" : "Alpplications used: #37\n\n+Launcher - Nova launcher\n\n+Widgets - https://play.google.com/store/apps/details?id=flawlesskwgt.kustom.pack \n\n+Icon Pack - https://play.google.com/store/apps/details?id=com.panotogomo.darktosca\n\n+Wallpaper - https://play.google.com/store/apps/details?id=com.stark.fluid\n\nTemplate by - https://twitter.com/vhthinh_at?s=09 ",
  "postid" : "-MYkLhERZIZ5az-K3VrQ",
  "postimage" : "https://firebasestorage.googleapis.com/v0/b/android-customization.appspot.com/o/Posted%20Pictures%2FAkZI2A4EZbMOHCHPqZp4h4hl2zL2%2F1618940184176.jpg?alt=media&token=f6ced789-be42-4d72-873f-8316ce557ff9",
  "publisher" : "AkZI2A4EZbMOHCHPqZp4h4hl2zL2"
},
"-MYkLpNkeHPEWwmYDgwN" : {
  "caption" : "Rate my Setup!!  ",
  "postid" : "-MYkLpNkeHPEWwmYDgwN",
  "postimage" : "https://firebasestorage.googleapis.com/v0/b/android-customization.appspot.com/o/Posted%20Pictures%2FAkZI2A4EZbMOHCHPqZp4h4hl2zL2%2F1618940216883.jpg?alt=media&token=59c0b67e-bd5c-4b6c-9c14-36e16676bf58",
  "publisher" : "AkZI2A4EZbMOHCHPqZp4h4hl2zL2"
}

]

Here if the posts have the publisherid same as the profile id get added into the postList List.

I am finding a solution for this for a long time, any help would be appreciated!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user9940099
  • 111
  • 5
  • Please edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data). – Alex Mamo Jun 02 '21 at 13:22
  • @AlexMamo Done, kindly recheck – user9940099 Jun 02 '21 at 13:29
  • Where in your object can I find that ID, for example, "125643"? – Alex Mamo Jun 02 '21 at 13:34
  • @AlexMamo Sir, I mentioned it as the order of posts 1 2 3 4 5 6 are getting rearranged differently as opposed to 6 5 4 3 2 1, it's not an object. – user9940099 Jun 02 '21 at 13:39
  • Try [this](https://stackoverflow.com/questions/47721971/how-to-arrange-firebase-database-data-in-ascending-or-descending-order). – Alex Mamo Jun 02 '21 at 13:43
  • The `reverse` should work. Given the JSON you shared, can you show the order in which you expect the posts to show up in the view? – Frank van Puffelen Jun 02 '21 at 14:35

0 Answers0