You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio Kotlin中如何将Firestore文档路径替换为用户可自定义的字符串

实现方案

1. 修改布局文件 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">

    <!-- 新增文档名输入框 -->
    <EditText
        android:id="@+id/et_doc_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入要查询的文档名称"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="32dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <!-- 新增查询按钮 -->
    <Button
        android:id="@+id/btn_query"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toBottomOf="@id/et_doc_name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

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

2. 修改逻辑文件 MainActivity.kt

核心是将原来写死的文档名apple替换为用户动态输入的内容,新增按钮点击触发查询的逻辑,同时增加用户可见的异常提示,修改后的完整代码如下:

package com.example.test

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
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<TextView>(R.id.value1)
        val value2 = findViewById<TextView>(R.id.value2)
        val value3 = findViewById<TextView>(R.id.value3)
        // 获取新增的输入框和按钮实例
        val etDocName = findViewById<EditText>(R.id.et_doc_name)
        val btnQuery = findViewById<Button>(R.id.btn_query)

        // 绑定按钮点击事件
        btnQuery.setOnClickListener {
            val docName = etDocName.text.toString().trim()
            // 先校验输入是否为空
            if (docName.isEmpty()) {
                Toast.makeText(this, "请输入要查询的文档名称", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            // 动态拼接要查询的文档路径
            val docRef = db.collection("storage").document(docName)
            docRef.get()
                .addOnSuccessListener { document ->
                    if (document.exists()) {
                        // 读取字段,为空时显示默认提示
                        value1.text = document.getString("value1") ?: "无数据"
                        value2.text = document.getString("value2") ?: "无数据"
                        value3.text = document.getString("value3") ?: "无数据"
                    } else {
                        Toast.makeText(this, "查询的文档不存在", Toast.LENGTH_SHORT).show()
                        // 清空之前的查询结果
                        value1.text = "无数据"
                        value2.text = "无数据"
                        value3.text = "无数据"
                    }
                }
                .addOnFailureListener { exception ->
                    Toast.makeText(this, "查询失败,请检查网络连接", Toast.LENGTH_SHORT).show()
                }
        }
        
        // 如果需要保留进入页面默认查询apple的逻辑,可以把原来的查询代码放在这里
    }
}

可选优化建议

  • 新增加载状态:查询过程中显示进度条,结束后隐藏,避免用户重复点击按钮
  • 输入规则校验:Firestore文档名不允许包含/、#、.等特殊字符,查询前先做校验可以减少不必要的无效请求
  • 快捷选择入口:如果可选的文档名数量有限,可以做成下拉选择框,不用用户手动输入,减少输入错误概率
  • 权限校验:如果数据库设置了读写权限,提前校验权限状态避免查询失败

内容的提问来源于stack exchange,提问作者CSBossmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 10:15:04