How to fetch data Firestore subcollection Kotlin?

Issue

This Content is from Stack Overflow. Question asked by terlan abaszade

I want to get data from Firebase Firestore subcollection for recyclerView. But I dont know if I write codes correctly or no. I also checked another variant with another collection without any subcollection and I see there I succesfully get data from Firestore. But with subcollection part which is i uploaded it with image below I can not fetch data and my recyclerView is empty.How can i fix that? Thanks in advance

Here is my Firebase Firestore collection
Here is my Firebase Firestore subcollection

And my codes

class Posts : AppCompatActivity(), RecAdapterPosts.ClickListener {
    var modalList : ArrayList<ModalImageList> = ArrayList()
    private lateinit var binding: PostsBinding
    private lateinit var auth: FirebaseAuth
    private var userAdapter = RecAdapterPosts(modalList, this)
    private var db = Firebase.firestore

    private var database: FirebaseFirestore = FirebaseFirestore.getInstance()






    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = PostsBinding.inflate(layoutInflater)
        val view = (binding.root)
        setContentView(view)

        auth = FirebaseAuth.getInstance()
        database = FirebaseFirestore.getInstance()
        gridPosts.adapter = userAdapter
        gridPosts.layoutManager = GridLayoutManager(this,2)
     get_information()

}

    private fun get_information() {
        val cu = auth.currentUser!!.uid
        database.collection("İmagesPosts",).document(cu!!).collection("ImagesList").orderBy("date",
            Query.Direction.DESCENDING
        ).addSnapshotListener { snaphot, exception ->
            if (exception != null) {
                Toast.makeText(this, exception.localizedMessage, Toast.LENGTH_LONG)
                    .show()
            } else {
                if (snaphot != null) {
                    if (!snaphot.isEmpty) {
                        val documents = snaphot.documents
                        modalList.clear()
                        for (document in documents) {
                            val imgUrl = document.get("downloadUs") as String
                            val imgName = document.get("imgName") as String



                            val download_post = ModalImageList(
                                imgUrl,
                                imgName,

                            )
                            modalList.add(download_post)


                        }
                    }
                    userAdapter.notifyDataSetChanged()


                }

            }
        }

    }



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?