Is there any simple way to set a 2 TextView dropdown to an AutoCompleteTextView.
There is android.R.layout.two_line_list_item
Which I couldn\'t find any exa
Converted Dwivedi Ji's answer to Kotlin. I had some issue with Android Studio's auto convert. Thus, spent some time to make it work.
Now it is working. In case anyone needs it (in my case, I am filtering street names):
class StreetsAdapter( private val mContext: Context,
private val viewResourceId: Int,
private val items: ArrayList) : ArrayAdapter(mContext, viewResourceId, items.toList()) {
private val itemsAll = items.clone() as ArrayList
private var suggestions = ArrayList()
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var v: View? = convertView
if (v == null) {
val vi = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
v = vi.inflate(viewResourceId, null)
}
val street: Street? = items[position]
if (street != null) {
val streetTitle = v?.findViewById(R.id.tvStreetTitle) as TextView?
streetTitle?.text = street.title
}
return v!!
}
override fun getFilter(): Filter {
return nameFilter
}
private var nameFilter: Filter = object : Filter() {
override fun convertResultToString(resultValue: Any): String {
return (resultValue as Street).title
}
override fun performFiltering(constraint: CharSequence?): FilterResults {
return if (constraint != null) {
suggestions.clear()
for (street in itemsAll) {
if (street.title.toLowerCase().startsWith(constraint.toString().toLowerCase())) {
suggestions.add(street)
}
}
val filterResults = FilterResults()
filterResults.values = suggestions
filterResults.count = suggestions.size
filterResults
} else {
FilterResults()
}
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
val filteredList = results?.values as ArrayList?
if (results != null && results.count > 0) {
clear()
for (c: Street in filteredList ?: listOf()) {
add(c)
}
notifyDataSetChanged()
}
}
}
}
And set your adapter:
val adapter = StreetsAdapter(this,
R.layout.item_street, //Your layout. Make sure it has [TextView] with id "tvStreetTitle"
arrayListOf() //Your list goes here
)
autoTextView.threshold = 1 //will start working from first character
autoTextView.setAdapter(adapter)