【问题标题】:Firebase UI Recyclerview is not updating when new items are inserted in the Firestore collection将新项目插入 Firestore 集合时,Firebase UI Recyclerview 不会更新
【发布时间】:2021-03-22 15:48:48
【问题描述】:

我希望我的 recyclerview 在集合发生变化时得到更新,因此根据 Firebase-ui 文档,我不需要做太多事情,因为如果我只是将 adapter.listening 添加到 onstart 和 onpause 活动方法,它会自动处理

我在 onstart 方法中设置了adapter.listening,但是当新项目添加到集合中时,UI 不会加载它,直到我暂停活动并调用 onstart 活动代码:

package net.raj.mushimushi.ui.comments

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver
import com.firebase.ui.firestore.FirestoreRecyclerOptions
import com.google.firebase.firestore.Query
import net.raj.mushimushi.databinding.ActivityCommentSectBinding
import net.raj.mushimushi.models.Comments

class CommentSectActivity : AppCompatActivity() {
    private lateinit var binding :ActivityCommentSectBinding
    private lateinit var adapter: CommentAdapter
    private val viewModel : CommentViewModel by viewModels()
    private lateinit var postId : String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCommentSectBinding.inflate(layoutInflater)
        postId = CommentSectActivityArgs.fromBundle(intent?.extras!!).postId


        binding.postCommentButton.setOnClickListener{
            val text = binding.commentInput.text.toString()
            viewModel.addComment(text, postId)
        }

        setUpRecyclerView()

        setContentView(binding.root)

    }



    private fun setUpRecyclerView() {
        val query = viewModel.commentCollection.whereEqualTo(
            "postId",
            postId
        ).orderBy("createdAt", Query.Direction.DESCENDING)
        val recyclerViewOptions = FirestoreRecyclerOptions.Builder<Comments>().setQuery(
            query,
            Comments::class.java
        ).build()
        adapter = CommentAdapter(recyclerViewOptions)

        binding.recyclerView2.adapter = adapter
        binding.recyclerView2.layoutManager = LinearLayoutManager(this)
    }

    override fun onStart() {
        super.onStart()
        adapter.startListening()
    }

    override fun onStop() {
        super.onStop()
        adapter.stopListening()
    }
}

这是我对 Firebase UI 提供的 recyclerview 适配器的实现

package net.raj.mushimushi.ui.comments

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.firebase.ui.firestore.FirestoreRecyclerAdapter
import com.firebase.ui.firestore.FirestoreRecyclerOptions
import net.raj.mushimushi.R
import net.raj.mushimushi.models.Comments

class CommentAdapter(options: FirestoreRecyclerOptions<Comments>) : FirestoreRecyclerAdapter<Comments,CommentAdapter.ViewHolder>(
        options
) {

    class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
        val commentBox : TextView = itemView.findViewById(R.id.commentBox)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_comment, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int, model: Comments) {
        holder.commentBox.text = model.text
    }


}

【问题讨论】:

  • 如果在onBindViewHolder 中添加Log.d("TAG", model.text),logcat 中是否会打印出一些内容?请编辑您的问题并将Comments 的内容和您的数据库结构添加为 JSON 文件或至少是屏幕截图。

标签: android firebase google-cloud-firestore android-recyclerview


【解决方案1】:

发现问题: 实际上我应该在firestore中创建一个复合索引,因为我一直在使用带有一系列子句的复合查询,所以我需要在firestore中为它设置索引

文档链接:https://firebase.google.com/docs/firestore/query-data/indexing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多