【问题标题】:How to allow the user to select data from firestore through specifying the document name?如何允许用户通过指定文档名称从firestore中选择数据?
【发布时间】:2021-09-02 21:07:32
【问题描述】:

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

作为数据库,我使用 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.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


    db.collection("storage")
        .get()
        .addOnSuccessListener { result ->
            for (document in result) {
                Log.d("exist", "${document.id} => ${document.data}")

                value1.text = document.getString("value1")
                value2.text = document.getString("value2")
                value3.text = document.getString("value3")

            }
        }
        .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" />

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="383dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="34dp"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="1dp">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your search here..." />
</com.google.android.material.textfield.TextInputLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

新错误的错误消息:

代码错误:

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    假设您的数据库结构如下所示:

    Firestore-root
      |
      --- storage (collection)
           |
           --- apple (document)
           |    |
           |    --- value1: "v1"
           |    |
           |    --- value2: "v2"
           |    |
           |    --- value3: "v3"
           |
           --- banana (document)
                |
                --- value1: "v1"
                |
                --- value2: "v2"
                |
                --- value3: "v3"
    

    要获取三个属性的值,例如对于“apple”文档,请使用以下代码行:

    val rootRef = FirebaseFirestore.getInstance()
    val storageRef = rootRef.collection("storage")
    storageRef.document("apple").get().addOnSuccessListener { document ->
        if (document != null) {
            val v1 = document.getString("value1")
            value1.text = v1
            val v2 = document.getString("value2")
            value2.text = v2
            val v3 = document.getString("value3")
            value3.text = v3
            Log.d(TAG, v1 + "/" + v2 + "/" + v3)
        } else {
            Log.d(TAG, "No such document")
        }
    }.addOnFailureListener { exception ->
        Log.d(TAG, "get failed with ", exception)
    }
    

    因此,您必须在构建引用时指定集合的​​名称以及文档的名称。您的 logcat 中的输出将是:

    v1/v2/v3
    

    【讨论】:

    • 谢谢亚历克斯!它适用于不同的文档,我可以在 logcat 中看到数字,但是当我运行应用程序时,我再也看不到数字了。我是否也必须更改 activity_main.xml 文件中的任何内容?
    • 您是否还知道一种让用户指定应该选择哪个文档的方法?那会很有帮助
    • 请尝试我更新的答案中的更改,您可以在其中将值分配给 TextViews。
    • 关于你的第二个问题,你应该将我的代码添加到一个方法中,而不是使用硬编码值“apple”,而是使用一个变量。并调用使用搜索的方法。试一试,看看它是否有效。
    • 如果您在实施时遇到困难,请在 StackOverflow 上使用自己的 MCVE 发布您尝试过的所有内容的新问题,以便我和其他 Firebase 开发人员可以帮助您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2016-04-08
    • 2017-10-04
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多