AutoCompleteTextView mActv = (AutoCompleteTextView) findViewbyId(R.id.m_actv);
ArrayAdapter<String> AutoCompleteAdapter = new ArrayAdapter<String>(this,
R.layout.dropdown_text, Names);
mActv.setAdapter(AutoCompleteAdapter);
Names is a String array.
Is it possible to get the index of the text selected from the dropdown??
Thank You.
Use List instead of Array. Implement onItemClickListener for AutoCompleteTextView, then use indexOf on your list to find the index of selected item. Show activity on this post.
For that, we first need to create an array named restaurants and add the names of various restaurants to show them as suggestions. Then we will implement the AdapterView. OnItemClickListener and set onItemClickListener() on the AutoCompleteTextView to get the user selected item value from the list.
An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
Simply add OnItemClickListener
(for clicked item) or OnItemSelectedListener
(for items selcted using a Trackball, Up/Down keys) to the AutoCompleteTextView
mActv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
String item = arg1.getItemAtPosition(pos);
//your stuff
}
});
Implement onItemClickListener for AutoCompleteTextView, then use indexOf on your list to find the index of selected item.
actvCity.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int index = cityNames.indexOf(actvCity.getText().toString());
// Do Whatever you want to do ;)
}
});
Try AutoCompleteTextView#getListSelection()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With