autocompletetextview setonitemselectedlistener not working
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem=autoCompleteTextView.getAdapter().getItem(position).toString();
Toast.makeText(getApplicationContext(),selectedItem , Toast.LENGTH_SHORT).show();
}
});
Just get the adapter of AutoCompleteTextView and use the position.
This is a duplicate of this question
However, you need to use AdapterView.OnItemClickListener() not OnItemSelectedListener
.
I tested it with success using the following code snippet. Credit to Vogella for the adapter stuff.
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autocomplete_textview);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
actv.setAdapter(adapter);
actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this,
adapter.getItem(position).toString(),
Toast.LENGTH_SHORT).show();
}
});