【发布时间】: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