如何替换documentPath的字符串,可以由用户在Android Studio Kotlin改变?



我正在尝试构建一个小应用程序,用户可以找到文档的值。

作为一个数据库,我使用Firestore数据库。我只有一个收藏"储物柜"。有许多文档,每个文档有3个值字段。我已经建立了一个活动,从数据库中获得3个值字段(见下文)。

我现在要做的是让用户通过选择不同的数据库文档名来决定他想要看到哪些值。

例如,如果用户输入"apple"在文本输入字段中,我想向他显示我保存在集合"storage"中的3个值。→文档"apple"→字段"value1";value2"one_answers"value3">。如果用户输入"香蕉";在文本输入字段中,我想向他显示我保存在集合"storage"中的3个值。→文档"banana"→字段"value1";value2"one_answers"value3">.

谁能给我一些建议如何实现这一点?不幸的是,我在别处找不到多少帮助。

MainActivity.kt

package com.example.test
import  android.content.ContentValues.TAG
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
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("storage").document("apple")
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)
}
}
}

activity_main.xml(看起来很可怕,但现在我只想解决的功能)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.585" />
<TextView
android:id="@+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.873"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.585" />
<TextView
android:id="@+id/value3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.162"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.586" />

</androidx.constraintlayout.widget.ConstraintLayout>

您需要引入一个新的字段,用户可以在其中输入或选择文档名称。一旦你有了它,你就可以读取它的值,并在访问数据库时使用它,像这样:

...
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 documentId = (findViewById(R.id.documentId) as TextView).getText().toString()
//                                     👆 Get the value the user entered here

val docRef = db.collection("storage").document(documentId)
//       👆 Then use it here
docRef.get()
.addOnSuccessListener { document ->
...

最新更新