Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

android - Get a firestore document id from autocompletetextview onItemClickListener items

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>)
}

I tried with the

itemId = suggestSnapshot.id

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...