【问题标题】:How to get the specific value from a database in Firebase using Kotlin in Android Studio?如何在 Android Studio 中使用 Kotlin 从 Firebase 中的数据库中获取特定值?
【发布时间】:2022-01-16 13:47:04
【问题描述】:

我很难从 firestore 的数据库中获取价值。

这是我的数据库的图片:

我要做的是从“添加帖子”文档中获取两个字段作为字符串形式,以将其传递到不同的 XML textView 以获取位置和描述。现在,当我运行它时,我的文本有些乱七八糟。

我想弹出标题位置,即“伊利诺伊州芝加哥”。我该怎么做?

这是我的活动:

package com.example.groupproject

import android.content.ContentValues.TAG

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase

class InspectPostActivity : AppCompatActivity() {

    private lateinit var locationTextView: TextView
    private lateinit var reference: DatabaseReference

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_inspect_post)

        // Linking all of the elements to their views
     
        locationTextView = findViewById(R.id.locationView)

        reference = FirebaseDatabase.getInstance().reference.child("Chicago, Illinois").child("locationTitle")

       
        // Sets the text to the database location
        locationTextView.text = reference.toString()
    }

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    Firebase 附带两个 NoSQL 数据库:实时数据库(一个 NoSQL JSON 数据库)和 Cloud Firestore(一个 NoSQL 文档/集合数据库)。虽然两者都是 Firebase 的一部分,但它们是完全独立的,其中一个的 API 无法在另一个上运行。

    您的问题和屏幕截图中的 google-cloud-firestore 标记表明您正在使用 Cloud Firestore,但您代码中的 FirebaseDatabase.getInstance() 正在尝试访问 Firebase 的实时数据库。要从 Firestore 获取文档,请改用 Firestore API for reading data

    【讨论】:

    • 嘿,我明白了。我发现我的错误是什么。我将发布我的解决方案。
    • 这是有道理的。我在网上查看了很多不同的视频和代码,所以我将其实现到其中,因为它是我能得到的最接近的。
    【解决方案2】:

    这是我发现如何做到这一点的方法。我摆脱了 Frank Van Puffelen 告诉我的那条线,而是实现了它。

    // Sets the text to the database location
            val docRef = db.collection("Add Post").document("Eau Claire, Wisconsin")
            docRef.get()
                .addOnSuccessListener { document ->
                    if (document != null) {
                        Log.d("Exists", "DocumentSnapshot data: ${document.data}")
    
                       // locationTextView.text = document.getString("locationTitle")
                        locationTextView.setText(document.getString("titleLocation"))
                        descriptionTextView.setText(document.getString("Description"))
                    }
                    else {
                        Log.d("noExist", "No Such Document")
                    }
                }
    

    我所要做的就是简单地在其中添加带有这个的行:

    document.getString(Name of Textfield) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-29
      相关资源
      最近更新 更多