android Firestore查询返回正确的值总数,但不返回实际值

zqdjd7g9  于 2023-05-21  发布在  Android
关注(0)|答案(1)|浏览(126)

我尝试使用从Firestore获取的数据填充回收器视图,遵循一些教程和此文档:https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#using-the-firestorerecycleradapter。从数据库中获取的数字(当前为3)似乎是正确的,但没有显示任何数据。此外,我已经在代码中使用日志消息来监视值,但实际值仍然为空。

我是这么做的:

在我的主片段中,我创建了一个适配器:

private var adapter: FirestoreCommanderNamesRecyclerAdapter? = null

然后开始监听适配器:

override fun onStart() {
        super.onStart()
        adapter!!.startListening()
        Log.d(TAG, "onStart -> start listening to the adapter")
    }

然后在onViewCreated中,我调用一个函数,它应该填充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")
        }
    
    }

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 = ""
    
    )

这就是数据的结构:

我的依赖项使用:

// 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"
</pre>

我已经检查过了

依赖项,用户身份验证(用户已登录),Firestore规则(我知道,这不安全,但对于测试来说,它应该可以做到这一点):rules_version = '2';服务云.firestore { match /databases/{database}/documents { match /{document=**} { allow read,write:如果为true; } } }
在设置(而不是查询)、查询本身、资源引用中放置虚拟文本值

问题

你知道为什么数据会被正确检索吗?

6pp0gazn

6pp0gazn1#

正如Doug所说,您的FirestoreCommanderNames的结构与您显示的文档中的数据不匹配,因此Firestore SDK无法自动为您Map值。
最直接的Map是:

data class FirestoreCommanderNames(
    val Name: String = ""
)

注意这里的Name是如何与数据库中的字段名完全匹配的?这就是Firebase知道如何在数据库和类之间Map的方式。
如果你还想将文档ID从元数据Map到你的类,你可以使用@DocumentId annotation来实现:

data class FirestoreCommanderNames(
    @DocumentId val ID: String = "",
    val Name: String = ""
)

相关问题