我很難從 firestore 的資料庫中獲取價值。
這是我的資料庫的圖片:

我想要做的是從“添加帖子”檔案中獲取兩個欄位作為字串形式,將其傳遞到不同的 XML textView 以獲取位置和描述。現在,當我運行它時,我對文本有一些胡言亂語。

我想讓 titleLocation 彈出,它只是“伊利諾伊州芝加哥”。我怎樣才能做到這一點?
這是我在我的活動中所擁有的:
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()
}
uj5u.com熱心網友回復:
Firebase 附帶兩個 NoSQL 資料庫:實時資料庫(NoSQL JSON 資料庫)和 Cloud Firestore(NoSQL 檔案/集合資料庫)。雖然兩者都是 Firebase 的一部分,但它們是完全獨立的,并且它們的 API 無法在 otehr 上運行。
google-cloud-firestore您的問題和螢屏截圖上的標簽表明您正在使用 Cloud Firestore,但FirebaseDatabase.getInstance()您的代碼中的 正在嘗試訪問 Firebase 的實時資料庫。要從 Firestore 獲取檔案,請改用Firestore API 讀取資料。
uj5u.com熱心網友回復:
這是我如何發現如何做到這一點的方法。我擺脫了 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")
}
}
我所要做的就是在其中添加帶有 this 的行:
document.getString(Name of Textfield)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/379731.html
標籤:安卓 火力基地 科特林 谷歌云firestore
