Get a firestore document id from autocompletetextv

2019-01-27 08:44发布

问题:

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.

回答1:

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 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.