I'm trying to use AutoCompleteTextView to implement a search through a list of item in a database. I able to create a drop-down menu when I insert a first char, but I'm not able to select one from the menu and get it to performe my search fuction. According with developer reference I create an ArrayAdapter to address the recources. Following an extract of my xml layout (R.layout.activity_test) and the structure to implement AutoCompleteTextView.
Layout activity_test
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TestActivity">
<AutoCompleteTextView
android:id="@+id/tvAutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Search"
android:inputType="textAutoComplete"
android:completionThreshold="1"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Implementation of AutoCompleteTextView
val itemList = ArrayList<String>()
itemList is successfully collected
val autoComplete: AutoCompleteTextView = findViewById(R.id.tvAutoComplete)
val adapter = ArrayAdapter(this,R.layout.activity_test,R.id.tvAutoComplete,itemList)
autoComplete.setAdapter(adapter)
autoComplete.setOnItemClickListener { parent, view, position, id ->
Log.d("Test ", itemList[position])
}
setOnItemClickListener not working.
To handle the click events of the item of its drop-down menu use these callbacks:
The one that you have mentioned in your answer is for handling click events of the
AutoCompleteTextViewwhich I think won't get invoked because it is a child ofEditText. InEditText, the click listeners don't get invoked.