You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore查询返回正确数量但无实际数据显示问题求助

问题:Firestore数据无法显示在RecyclerView中

我正尝试通过Firestore获取数据填充RecyclerView,参考了FirebaseUI官方文档。目前查询能返回正确的数据条数(当前为3条),但RecyclerView中无实际数据显示,且日志监测显示实际值为空。


实现代码

主Fragment相关代码

  • 适配器声明:
private var adapter: FirestoreCommanderNamesRecyclerAdapter? = null
  • onStart方法:
override fun onStart() {
    super.onStart()
    adapter!!.startListening()
    Log.d(TAG, "onStart -> start listening to the adapter")
}
  • 初始化RecyclerView的函数:
private fun invokeRecyclerView(){
    val rootRef = FirebaseFirestore.getInstance()
    val query = rootRef!!.collection("commanders")
    //TODO: Here is something off
    val options = FirestoreRecyclerOptions.Builder<FirestoreCommanderNames>().setQuery(query, FirestoreCommanderNames::class.java).build()

    adapter = FirestoreCommanderNamesRecyclerAdapter(options)
    binding.fragmentCommanderSettingsRecyclerview.layoutManager = LinearLayoutManager(this.context)
    binding.fragmentCommanderSettingsRecyclerview.adapter = adapter
}

适配器类 FirestoreCommanderNamesRecyclerAdapter

class FirestoreCommanderNamesRecyclerAdapter constructor(options: FirestoreRecyclerOptions<FirestoreCommanderNames>):
    FirestoreRecyclerAdapter<FirestoreCommanderNames, FirestoreCommanderNamesViewHolder>(options){

    private val TAG = "CmdrNamesRecyclerAdapte"

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): FirestoreCommanderNamesViewHolder {
        Log.d(TAG, "onCreateViewHolder started")
        val view = LayoutInflater.from(parent.context).inflate(R.layout.settings_name_list_item, parent, false)
        return FirestoreCommanderNamesViewHolder(view)
    }

    override fun onBindViewHolder(
        holder: FirestoreCommanderNamesViewHolder,
        position: Int,
        model: FirestoreCommanderNames
    ) {
        Log.d(TAG, "onBindViewHolder started")
        Log.d(TAG, "onBindViewHolder Model Commander Name: ${model.commander_names_ID}, position: $position")
        holder.setCommanderName(model.commander_names_ID )
    }

    override fun onError(e: FirebaseFirestoreException) {
        super.onError(e)
        Log.d(TAG, "Error: $e")
    }
}

ViewHolder类 FirestoreCommanderNamesViewHolder

class FirestoreCommanderNamesViewHolder constructor(private val view: View) : RecyclerView.ViewHolder(view){

    private val TAG = "CmdrNamesViewHolder"

    internal fun setCommanderName(commanderNames: String){
        Log.d(TAG, "Viewholder Value: $commanderNames")
        val textView = view.findViewById<TextView>(R.id.settings_list_item_textview_name)
        textView.text = commanderNames
    }
}

数据类 FirestoreCommanderNames

data class FirestoreCommanderNames(
    val commander_names_ID: String = "",
    val commander_names_NAME: String = ""
)

Firestore数据结构

Firestore数据结构

使用的依赖

// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:32.0.0')

// When using the BoM, don't specify versions in Firebase dependencies
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation("com.google.firebase:firebase-crashlytics-ktx")
implementation("com.google.firebase:firebase-auth-ktx")
implementation("com.google.firebase:firebase-firestore-ktx")
implementation 'com.google.firebase:firebase-core:21.1.1'
implementation("com.google.firebase:firebase-perf-ktx")
implementation("com.google.firebase:firebase-config-ktx")
implementation("com.google.firebase:firebase-messaging-ktx")
implementation 'com.google.firebase:firebase-database-ktx'
implementation 'com.google.firebase:firebase-storage-ktx'

// Firebase UI
implementation 'com.firebaseui:firebase-ui-auth:8.0.2'
implementation 'com.firebaseui:firebase-ui-database:8.0.2'
implementation 'com.firebaseui:firebase-ui-firestore:8.0.2'
implementation 'com.firebaseui:firebase-ui-storage:8.0.2'

implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "androidx.recyclerview:recyclerview:1.3.0"

已排查内容

  • 依赖版本兼容性
  • 用户认证状态(已正常登录)
  • Firestore规则(已设置为全开放测试)
  • 测试过在ViewHolder中设置假文本,可正常显示
  • 确认查询语句指向正确的集合
  • 验证布局资源ID引用无误

请问有人知道为什么无法正确获取并显示实际数据吗?

内容的提问来源于stack exchange,提问作者prextor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 19:47:50