【问题标题】:How can I access to documents's random id to attach it include documents's fields?如何访问文档的随机 ID 以附加它包括文档的字段?
【发布时间】:2018-09-07 19:33:49
【问题描述】:

enter image description here

我有一个RecyclerView 显示来自不同子集合的大量照片。我只想当用户 longclick 可以编辑或删除时,我从 Firesbase Storage 获得照片的参考并在其上附加了 uid。所以如果文档上的 uid == 当前用户 uid 他可以删除或编辑。 问题是我无法到达照片的路径,因为它是从不同的子集合中获取的! 你能帮帮我吗?

科特林

class ViewHolder(view: View, var my_data: Data? = null) : RecyclerView.ViewHolder(view) {
        init {
            view.setOnClickListener {
                val intent = Intent(view.context, More::class.java)
                intent.putExtra("description", my_data?.description)
                intent.putExtra("photo", my_data?.photo_uri)
                intent.putExtra("site", my_data?.site)
                view.context.startActivity(intent)
            }

            view.setOnLongClickListener {
                val popUp = PopupMenu(view.context, it)
                popUp.inflate(R.menu.edit_delete)
                popUp.setOnMenuItemClickListener { item ->
                    when (item.itemId) {
                        R.id.edit -> {
                            true
                        }
                        R.id.delete -> {

                            var dp: FirebaseFirestore = FirebaseFirestore.getInstance()
                            dp.collection("Fraise")
                                    .document()
                                    .delete()
                                    .addOnCompleteListener {
                                    }
                            Toast.makeText(view.context, "Note has been deleted!", Toast.LENGTH_SHORT).show()
                            true
                        }
                        else -> false  }
                popUp.show(); true
            }

【问题讨论】:

    标签: android firebase kotlin google-cloud-firestore


    【解决方案1】:

    假设您的数据库结构与我为您提供答案的其他 question 保持一致,并且您正在使用名为 Fraise 的模型类来删除/编辑来自特定用户的照片,请使用以下命令代码:

    val uid = FirebaseAuth.getInstance().currentUser!!.uid
    val rootRef = FirebaseFirestore.getInstance()
    val query = rootRef.collection("Fraise").whereEqualTo("uid", uid)
    query.get().addOnCompleteListener(OnCompleteListener<QuerySnapshot> { task ->
        if (task.isSuccessful) {
            for (document in task.result) {
                val fraise = document.toObject(Fraise::class.java!!)
                fraise!!.setPhoto_url(new_photo_uri)
                rootRef.collection("Fraise").document(document.id).set(fraise)
            }
        }
    })
    

    如果没有模型类,您可以使用Map 进行更新,如下所示:

    val uid = FirebaseAuth.getInstance().currentUser!!.uid
    val rootRef = FirebaseFirestore.getInstance()
    val query = rootRef.collection("Fraise").whereEqualTo("uid", uid)
    query.get().addOnCompleteListener(OnCompleteListener<QuerySnapshot> { task ->
        if (task.isSuccessful) {
            for (document in task.result) {
                val new_photo_uri = HashMap<String, Any>()
                new_photo_uri["photo_uri"] = new_photo_uri
                rootRef.collection("Fraise").document(document.id).set(new_photo_uri, SetOptions.merge())
            }
        }
    })
    

    【讨论】:

    • 嗨玛丽亚姆!我的回答对您有任何帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2023-01-01
    • 2021-04-24
    • 2020-09-09
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多