【问题标题】:How to retrieve data from Firestore that stored as an Array and set them as EditText values in Kotlin?如何从存储为数组的 Firestore 中检索数据并将它们设置为 Kotlin 中的 EditText 值?
【发布时间】:2021-07-03 10:34:28
【问题描述】:

我使用以下代码将一些数据作为数组存储在Firestore 中。现在,我想获取这些值并将它们一一放入EditTexts。我该怎么做?

    private fun addZipToFirebase() {

    val zipList = createListOfZipCodes()

    mFireStore.collection(Constants.USERS)
        .document(FirestoreClass().getCurrentUserID())
        .update("zip_codes", zipList)

        .addOnSuccessListener {
            Toast.makeText(
                this@AssignZIPCodeActivity,
                "Zip Codes updates successfully",
                Toast.LENGTH_SHORT
            ).show()
        }

        .addOnFailureListener { exception ->
            Log.e(
                javaClass.simpleName,
                exception.message,
                exception
            )
        }
}

编辑:

我正在尝试使用以下代码来获取数据。我想要每个EditText(etPinCodeOne、etPinCodeTwo、etPinCodeThree 等)字段名称 zip_codes (在屏幕截图中)下的每个邮政编码。但是通过以下代码,我得到的是EditText 中的所有邮政编码。非常喜欢 [123456, 789456, 132645,798654, 798654, 799865, 764997, 497646, 946529, 946585]。我想要每个EditText 中的每个代码。

   private fun getZipCodesFromFirebase() {

        mFireStore.collection(Constants.USERS)
            .document(FirestoreClass().getCurrentUserID())
            .get()
            .addOnSuccessListener { document ->
                val list: ArrayList<String> = ArrayList()
                list.add(document["zip_codes"].toString())
Toast.makeText(this@AssignZIPCodeActivity,list.toString(),Toast.LENGTH_SHORT).show()

            binding.etZipCodeOne.setText(list[0])

                }

            }

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 可以找到文档获取文档here。你能分享一下你迄今为止尝试过的任何代码吗?
  • 由于我想要获取的值存储为 ArrayList 并且我想将它们作为 ArrayList 获取`我不知道如何编写。我已经浏览了您提供的链接,但问题又是数组。
  • 这里有一个answer,可能会对您有所帮助。您只需要在代码中定义一个数组:ArrayList&lt;String&gt; list = (ArrayList&lt;String&gt;) document.get("ip_range");
  • 你现在能看看我的问题吗,我已经更新了一些代码和我的firebase数据库结构。
  • 我试过 .document(FirestoreClass().getCurrentUserID()).get("zip_codes") ,就像你上一条评论一样。我收到错误 Type mismatch: inferred type is String but Source was expected 。请注意,我想要Kotlin 的代码。

标签: kotlin google-cloud-firestore


【解决方案1】:

为了能够获取zip_codes 数组,您需要在 User 类中拥有一个名为 zip_codes 的属性,该属性需要声明为 List 类型:

val zip_codes: List<String>

现在,要相应地获取它,请使用以下代码行:

val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseFirestore.getInstance()
val usersRef = rootRef.collection("users")
val uidRef = usersRef.document(uid)
uidRef.get().addOnCompleteListener { task ->
    if (task.isSuccessful) {
        val document = task.result
        if (document.exists()) {
            val zip_codes = document.toObject(User::class.java).zip_codes
            //Do what you need to do with your list
        } else {
            Log.d(TAG, "No such document")
        }
    } else {
        Log.d(TAG, "get failed with ", task.exception)
    }
}

由于您获得多个邮政编码,因此您应该考虑使用 ListView,甚至更好的是 RecyclerView,而不是 EditTexts。

【讨论】:

  • 它就像一个魅力。我刚刚用followingRef 替换了usersRef。此外,task.getResult()result 由 Android Studio 建议。所有开发人员都很幸运有像您这样的导师。非常感谢。正如你所说,我将使用RecyclerView。如果我所做的更改是正确的,那么也请更新您的答案。
  • 是的,我刚刚用正确的数据更新了答案。是的,result 是类中的一个属性,因此使用它而不是方法调用是有意义的。不客气!
猜你喜欢
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
相关资源
最近更新 更多