Search code examples
androidandroid-recyclerviewandroid-viewmodel

Retrieving data by id from the card listed with Recyclerview


I post an ad in my app. I pull the ads as a list from the firebase and list them with the recycler adapter. Using ViewModel. In each ad, there is the ID of the person who published the ad. My problem; I want to show the information about the advertiser in the card, but I could not find how to extract the data with the advertiser's ID in the adapter and connect it to each card. How should I go about this? I thought about transferring all users along with my ad list to the adapter and finding them from there, but it doesn't seem to be the right method.


Solution

  • I found a solution to the problem. I access my repository in Adapter as follows;

        val repo: FirebaseRepoInterFace
    
        @EntryPoint
        @InstallIn(SingletonComponent::class)
        interface RepoEntryPoint {
            fun getRepo(): FirebaseRepoInterFace
        }
    
        init {
            val repoEntryPoint =
                EntryPointAccessors.fromApplication(context, RepoEntryPoint::class.java)
            repo = repoEntryPoint.getRepo()
        }
    

    Then, with the method in my repository, I bring the different users with their ids for each card.

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            post.userId?.let { id ->
                repo.getUserDataByDocumentId(id).addOnSuccessListener {
                    holder.binding.user = it.toObject(UserModel::class.java)
                }
            }
    }