从autocompletetextview onItemClickListener项目获取Firestore文档ID [英] Get a firestore document id from autocompletetextview onItemClickListener items
问题描述
我的问题非常简单明了.我已经从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.
推荐答案
要解决此问题,请移动设置添加适配器的行,并将列表器附加在回调内部,如下所示:
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屋!