[英]Android Kotlin AutoCompleteTextView item shows whole item Object when clicked
So I have an AutoCompleteTextView that its dropdownlist populated with data from an array of object that formed from two different arrays by using the zip
function. So I have an AutoCompleteTextView that its dropdownlist populated with data from an array of object that formed from two different arrays by using the
zip
function.
What I want is that when each item of the AutoCompleteTextView dropdown item is clicked, it'll return one of the attribute of the clicked item object.我想要的是,当单击 AutoCompleteTextView 下拉项的每个项时,它将返回单击项 object 的属性之一。
(This question is kind of similar with this question, however I still do not understand what to do even after reading the comments). (这个问题与这个问题有点相似,但是即使在阅读了评论后我仍然不明白该怎么做)。
This is the snippet code from the Fragment where AutoCompleteTextView newProcodeEditFormEditText
is declared:这是声明 AutoCompleteTextView
newProcodeEditFormEditText
的 Fragment 的代码片段:
val procodeRecommendationList = allProductCodeList.zip(allProductNameList)
.map { (allProductCodeList, allProductNameList) -> ProcodeRecommendationListDataClass(allProductCodeList, allProductNameList) }
//This is where the created list of object being logged
Log.i("Order", "Created List of objects: $procodeRecommendationList")
//Set up adapter for these recommender
val recommendedProductNameListAdapter = ArrayAdapter((activity as AppCompatActivity), android.R.layout.simple_list_item_1, allProductNameList)
val recommendedProductCodeListAdapter = ProcodeRecommendationAdapter(activity as AppCompatActivity, R.layout.procode_recommendation_list_item_layout, procodeRecommendationList.toTypedArray())
newProcodeEditFormEditText.threshold = 1
newProductNameEditFormEditText.threshold = 1
//Connect the adapter to each editText
newProcodeEditFormEditText.setAdapter(recommendedProductCodeListAdapter)
newProductNameEditFormEditText.setAdapter(recommendedProductNameListAdapter)
//This is the onclick listener for the items in the AutoCompleteTextView Dropdown
newProcodeEditFormEditText.setOnItemClickListener { parent, view, position, id ->
val selectedItem = parent.getItemAtPosition(position)
Toast.makeText((activity as AppCompatActivity), "Selected : ".plus(selectedItem), Toast.LENGTH_LONG).show()
Log.i("Order", "Selected item: ${selectedItem}")
}
This is the created List of object procodeRecommendationList
as shown in the LogCat:这是创建的 object
procodeRecommendationList
列表,如 LogCat 所示:
And this is what the clicked item returned as shown in the LogCat (Stored in the variable selectedItem
):这就是单击的项目返回的内容,如 LogCat 所示(存储在变量
selectedItem
中):
As expected, it shown in the AutoCompleteTextView like this:正如预期的那样,它在 AutoCompleteTextView 中显示如下:
And this is the Custom adapter i use for showing the items in the AutoCompleteTextView Dropdown:这是我用于在 AutoCompleteTextView 下拉列表中显示项目的自定义适配器:
class ProcodeRecommendationAdapter(private val c: Context,
@LayoutRes private val layoutResource: Int,
private val items: Array<ProcodeRecommendationListDataClass>)
: ArrayAdapter<ProcodeRecommendationListDataClass>(c, layoutResource, items) {
var filteredRecommendations: List<ProcodeRecommendationListDataClass> = listOf()
override fun getCount(): Int = filteredRecommendations.size
override fun getItem(position: Int): ProcodeRecommendationListDataClass = filteredRecommendations[position]
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = convertView ?: LayoutInflater.from(c).inflate(layoutResource, parent, false)
view.tvProcode.text = filteredRecommendations[position].procode
view.tvProname.text = filteredRecommendations[position].productName
return view
}
override fun getFilter(): Filter {
return object : Filter() {
override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) {
@Suppress("UNCHECKED_CAST")
filteredRecommendations = filterResults.values as List<ProcodeRecommendationListDataClass>
notifyDataSetChanged()
}
override fun performFiltering(charSequence: CharSequence?): FilterResults {
val queryString = charSequence?.toString()?.toLowerCase()
val filterResults = FilterResults()
filterResults.values = if (queryString == null || queryString.isEmpty())
items.asList()
else
items.filter {
it.procode?.toLowerCase(Locale.ROOT)!!.contains(queryString)
}
return filterResults
}
}
}
}
And this is the data class I use:这是我使用的数据 class:
data class ProcodeRecommendationListDataClass(
val procode: String?,
val productName: String?
)
So far it has no problem in populating the AutoCompleteTextView dropdown items.到目前为止,填充 AutoCompleteTextView 下拉项没有问题。
What I want is that I want to get the procode
attribute so it'll only show 0100009
in the AutoCompleteTextView instead of ProcodeRecommendationListDataClass(procode=0100009, productName=TARIVID OTIC 3MG EAR DROPS 5ML)
when the item is clicked.我想要的是我想要获取
procode
属性,因此它只会在 AutoCompleteTextView 中显示0100009
而不是ProcodeRecommendationListDataClass(procode=0100009, productName=TARIVID OTIC 3MG EAR DROPS 5ML)
单击该项目时。
How can I achieve something like that?我怎样才能实现这样的目标? Where should I change in the adapter code?
我应该在哪里更改适配器代码? If There's any detail I miss, just let me know!
如果我错过了任何细节,请告诉我!
Edit:编辑:
If anyone needs the AutoCompleteTextview item layout, here:如果有人需要 AutoCompleteTextview 项目布局,请在此处:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/tvProcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvProname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvProcode" />
</androidx.constraintlayout.widget.ConstraintLayout>
When you select any item from AutoCompleteTextView
it will be returning object of ProcodeRecommendationListDataClass
当您 select 来自
AutoCompleteTextView
的任何项目时,它将返回 ProcodeRecommendationListDataClass 的ProcodeRecommendationListDataClass
You need to access it like below code您需要像下面的代码一样访问它
newProcodeEditFormEditText.setOnItemClickListener { parent, view, position, id ->
val selectedItem = parent.getItemAtPosition(position) as ProcodeRecommendationListDataClass
Toast.makeText((activity as AppCompatActivity), "Selected : ".plus(selectedItem.procode), Toast.LENGTH_LONG).show()
Toast.makeText((activity as AppCompatActivity), "Selected : ".plus(selectedItem.productName), Toast.LENGTH_LONG).show()
newProcodeEditFormEditText.setText(selectedItem.procode)
Log.i("Order", "Selected item: ${selectedItem}")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.