通过Recyclerview列出的卡中的id检索数据

问题描述 投票:0回答:1

我在我的应用程序中发布了广告。 我从 firebase 中将广告作为列表拉出,并使用回收器适配器列出它们。使用视图模型。 每个广告中都有发布该广告的人的 ID。 我的问题; 我想在卡中显示有关广告商的信息,但我找不到如何提取适配器中广告商 ID 的数据并将其连接到每张卡。我该怎么办? 我考虑过将所有用户连同我的广告列表一起传输到适配器并从那里找到他们,但这似乎不是正确的方法。

android android-recyclerview android-viewmodel
1个回答
0
投票

我找到了解决问题的方法。 我按如下方式访问适配器中的存储库;

    val repo: FirebaseRepoInterFace

    @EntryPoint
    @InstallIn(SingletonComponent::class)
    interface RepoEntryPoint {
        fun getRepo(): FirebaseRepoInterFace
    }

    init {
        val repoEntryPoint =
            EntryPointAccessors.fromApplication(context, RepoEntryPoint::class.java)
        repo = repoEntryPoint.getRepo()
    }

然后,使用我的存储库中的方法,我为每张卡提供不同的用户及其 ID。

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        post.userId?.let { id ->
            repo.getUserDataByDocumentId(id).addOnSuccessListener {
                holder.binding.user = it.toObject(UserModel::class.java)
            }
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.