【问题标题】:Can't retrieve data from firebase database to show inside my textbox无法从 firebase 数据库中检索数据以显示在我的文本框中
【发布时间】:2020-01-07 03:06:38
【问题描述】:

我试图从 firebase 云数据库中获取数据,但数据没有显示出来。

This is the code I try to retrieve data

private fun updateUI(currentUser: FirebaseUser?){
    if(currentUser!=null){
        fstore.collection("users")
            .document(auth.currentUser!!.uid)
            .get()
            .addOnCompleteListener { task->
                task.result!!.get("name") == name.text.toString();
                task.result!!.get("email") == email.text.toString();

                Toast.makeText(baseContext, "Haha", Toast.LENGTH_LONG).show()
            }
    }else{
        Toast.makeText(baseContext, "fail", Toast.LENGTH_LONG).show()
    }
}

Calling UpdateUI() form activity

class MainActivity : AppCompatActivity() {

lateinit var name: TextView
lateinit var email: TextView

private lateinit var fstore: FirebaseFirestore
private lateinit var auth : FirebaseAuth

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

    name = findViewById(R.id.name)
    email = findViewById(R.id.email)

    auth   = FirebaseAuth.getInstance()
    fstore = FirebaseFirestore.getInstance()

    updateUI(auth.currentUser)
}

firebase 云数据库:

我尝试过的android模拟器:

【问题讨论】:

  • 请在您的问题中包含您的代码,而不是发布代码的图像。
  • 你有什么错误吗?

标签: android firebase kotlin google-cloud-firestore


【解决方案1】:

您的文档ID分配错误

.document(doucumentID) //Not user ID
//Here your document id is 
//JXoTcqxIVENTSF2..... 
//As I can see your firestore image

并且您以错误的方式将数据分配给TextView

name.text = "value you want to assign" 

所以像这样编辑你的代码

private fun updateUI(currentUser: FirebaseUser?){
    if(currentUser!=null){
        fstore.collection("users")
            .document("JXoTcqxIVENTSF2..")
            //can not see full id in image so I use '....'
            //replace it with full id from firestore
            .get()
            .addOnCompleteListener { task->

                name.text = task.result!!.get("name").toString()
                email.text = task.result!!.get("email").toString()

                Toast.makeText(baseContext, "Haha", Toast.LENGTH_LONG).show()
            }
    }else{
        Toast.makeText(baseContext, "fail", Toast.LENGTH_LONG).show()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 2018-11-10
    相关资源
    最近更新 更多