【问题标题】:How to replace a database call (firestore) to a document by a string in Android Kotlin?如何用Android Kotlin中的字符串替换对文档的数据库调用(firestore)?
【发布时间】:2021-09-05 14:03:42
【问题描述】:

我正在尝试构建两个应该传递给 firebase 的 autotextView,以在那里查找相应的 documentId 以显示该文档的 3 个值。

我的问题是我无法将第一部分(autoTextView)的 documentId 连接到第二部分(firebase 查询)。这两个单独的部分工作正常,但两者之间通过 documentId 字符串的连接似乎不起作用。

我的问题是我不知道如何调用文档名称(另存为“enteredText”)

排队

    val docRef = db.collection("strecken").document("enteredText")

有谁知道问题出在哪里/我该如何解决?

    package com.example.servus
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.util.Log
    import android.widget.TextView
    import com.google.firebase.firestore.ktx.firestore
    import com.google.firebase.ktx.Firebase
    
    import android.view.View
    import android.widget.ArrayAdapter
    import android.widget.AutoCompleteTextView
    import android.widget.Button
    import android.widget.Toast
    import com.google.firebase.firestore.FieldPath.documentId
    
    
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        // autotextView for Start

        val autotextView
                = findViewById<AutoCompleteTextView>(R.id.autoTextViewStart)
        val autotextViewZiel
                = findViewById<AutoCompleteTextView>(R.id.autoTextViewZiel)


    // Get the array of languages
    val languages
            = resources.getStringArray(R.array.Languages)
    // Create adapter and add in AutoCompleteTextView
    val adapter
            = ArrayAdapter(this,
        android.R.layout.simple_list_item_1, languages)
    autotextView.setAdapter(adapter)
    autotextViewZiel.setAdapter(adapter)


    val button
            = findViewById<Button>(R.id.btn); if (button != null)
    {
        button.setOnClickListener(View.OnClickListener {
            val enteredText = autotextView.getText()
            Toast.makeText(this@MainActivity, enteredText, Toast.LENGTH_SHORT).show()
        })
    }


        // get values from firestore

        val db = Firebase.firestore

        val value1= findViewById(R.id.value1) as TextView
        val value2= findViewById(R.id.value2) as TextView
        val value3= findViewById(R.id.value3) as TextView
        val docRef = db.collection("strecken").document("enteredText")


        //** old hard coded way** val docRef = db.collection("storage").document("apple")
        val docRef = db.collection("storage").document(documentId)

        docRef.get()
           .addOnSuccessListener { document ->
               if (document !=null) {
                    Log.d("exist", "DocumentSnapshot data: ${document.data}")

                   value1.text = document.getString("value1")
                   value2.text = document.getString("value2")
                   value3.text = document.getString("value3")
               } else {
                   Log.d("noexist", "No such docoument")
               }
           }
           .addOnFailureListener { exception ->
                Log.w("notexisting", "Error getting documents.", exception)
           }
    }
}

【问题讨论】:

    标签: android kotlin google-cloud-firestore


    【解决方案1】:

    您需要拉起enteredText 变量,以便它在您需要的代码中可用:

    var enteredText: String // ? Add this declaration
    
    val button
            = findViewById<Button>(R.id.btn); if (button != null)
    {
        button.setOnClickListener(View.OnClickListener {
            enteredText = autotextView.getText().toString() // ? assign (but don't declare) it here
            Toast.makeText(this@MainActivity, enteredText, Toast.LENGTH_SHORT).show()
        })
    }
    
    
    // get values from firestore
    
    val db = Firebase.firestore
    
    val value1= findViewById(R.id.value1) as TextView
    val value2= findViewById(R.id.value2) as TextView
    val value3= findViewById(R.id.value3) as TextView
    val docRef = db.collection("strecken").document(enteredText) // ? So that you can use it here
    

    【讨论】:

    • 感谢您的帮助!我已经这样做了,但知道我收到错误“由于可能的重新分配而禁止捕获的值初始化”,用于“输入的文本 [= ...] 和”类型不匹配。必需:找到的字符串:可编辑!" for [... =] "autotextView.getText()"。你知道问题可能是什么吗?
    • 啊,是的,看来变量应该是var 而不是val。顺便说一句:我强烈建议搜索这样的错误消息,因为我也从中得到了解决此错误的方法。
    • 谢谢,我现在更正了两个错误,所以它:“var enterText:String”和“enteredText = autotextView.getText().toString()”。唯一剩下的问题是我的数据库现在引用说“必须初始化变量'enteredText'”。你知道我能做些什么来解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多