【问题标题】:How to replace documentPath by a string that can be changed by a user in Android Studio Kotlin?如何用用户可以在 Android Studio Kotlin 中更改的字符串替换 documentPath?
【发布时间】:2021-09-03 20:57:05
【问题描述】:

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

作为数据库,我使用的是 Firestore 数据库。我只有一个集合“存储”,其中包含许多文档和每个文档 3 个值字段。我已经构建了一个从数据库中获取 3 个值字段的活动(见下文)。

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

例如,如果用户在文本输入字段中键入“apple”,我想向他显示我保存在 collection “storage” -> document “apple” -> fields 中的 3 个值”值1”、“值2”和“值3”。如果用户在文本输入字段中输入“banana”,我想向他展示我保存在 collection “storage” -> document “banana” -> fields “value1”、“value2”中的 3 个值" 和 "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>

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

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

    ...
    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 ->
    ...
    

    【讨论】:

      猜你喜欢
      • 2013-07-02
      • 1970-01-01
      • 2022-01-16
      • 2021-04-06
      • 1970-01-01
      • 2016-03-26
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多