如何替换一个数据库调用(firestore)到一个文档的字符串在Android Kotlin?



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

我的问题是,我不能将第一部分(autoTextView)的文档id连接到第二部分(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)
}
}
}

您需要拉出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

相关内容

  • 没有找到相关文章

最新更新