How to fetch data Firestore subcollection Kotlin?

Viewed 34

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()
                }
            }
        }
    }
0 Answers
Related