从 autocompletetextview onItemClickListener 项目中获取 Firestore 文档 ID [英] Get a firestore document id from autocompletetextview onItemClickListener items

查看:24
本文介绍了从 autocompletetextview onItemClickListener 项目中获取 Firestore 文档 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题非常简单明了.我已经从 firestore cloud-firestore 数据库中获取了数据,它在 AutoCompleteTextView 中得到了很好的建议,并且可以点击.但是,我想获取所选项目的 firebase cloud-firestore 文档 ID.经烘烤测试

My problem is very simple and clear. I already did fetch data from the firestore cloud-firestore database, it is being suggested in AutoCompleteTextView very well, and clickable. However, I want to get the firebase cloud-firestore document id of the selected item. Tested with toasting it

private var autoComplete: ArrayAdapter<String>? = null
private var itemId: String? = null

override fun onCreate(savedInstanceState: Bundle?) {
    readData(object: MyCallback {
        override fun onCallback(value: ArrayAdapter<String>) {
            Log.d(TAG, "The list has: " + value.count.toString() + " items.")
        }
    })

    textCurrentSearch.setAdapter(autoComplete)

    textCurrentSearch.onItemClickListener = OnItemClickListener { parent, view, position, id ->

        showShortToast(this@NewOrderActivity, "Item on cloud-firestore id: " + itemId!! + "Item on ArrayAdapter id: " + id)
    }
}

fun showShortToast(context: Context, message: String) {
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}

private fun readData(myCallback : MyCallback) {
    Log.d(TAG, "Before attaching the listener!")

    mFirebaseFirestore.collection("tblProductItems").get().addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d("TAG", "Inside onComplete function!")
            for (document in task.result!!) {
                val name = document.data["name"].toString()

                itemId = document.id

                autoComplete?.add(name)
            }

            myCallback.onCallback(autoComplete!!)

        } else showShortToast(this@NewOrderActivity, task.exception!!.toString())
    }.addOnSuccessListener {
        showShortToast(this@NewOrderActivity, "")
    }

    Log.d(TAG, "After attaching the listener!")
}

interface MyCallback {
    fun onCallback(value: ArrayAdapter<String>)
}

我试过

itemId = suggestSnapshot.id

但它没有让我获得所选产品项目的 ID.有建设性的请帮忙,谢谢.

but it does not get me the id of the selected product item doument. Kindly help with anything constructive, thank you.

推荐答案

要解决这个问题,请移动您设置 addapter 和在回调中附加列表器的行,如下所示:

To solve this, please move the lines where you are setting the addapter and you are attaching the lister right inside the callback like so:

override fun onCreate(savedInstanceState: Bundle?) {
    mFirebaseFirestore.collection("tblProductItems").addSnapshotListener { querySnapshot, exception ->
        if (exception != null) {
            showShortToast(this@NewOrderActivity, exception.toString())
        }

        for (suggestSnapshot in querySnapshot!!.documents) {
            val suggestion = suggestSnapshot.getString("name")

            itemId = suggestSnapshot.id

            //Add the retrieved string to the list
            autoComplete?.add(suggestion)
        }
        textCurrentSearch.setAdapter(autoComplete)
        textCurrentSearch.onItemClickListener = OnItemClickListener { parent, view, position, id ->
            showShortToast(this@NewOrderActivity, "Item on cloud-firestore id: " + itemId!! + "Item on ArrayAdapter id: " + id)
        }
    }
}

Firebase API 是异步的,这意味着数据只有在您等待时才可用.有关更多信息,我建议您还查看我的回答 发布.

Firebase API is asynchronous meaning that the data is available only if you wait for it. For more informations, I recommend you also see my answer from this post.

这篇关于从 autocompletetextview onItemClickListener 项目中获取 Firestore 文档 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆