Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the index of the Text selected in an AutoCompleteTextView

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.

like image 513
Archie.bpgc Avatar asked Nov 20 '12 06:11

Archie.bpgc


People also ask

How do I get the selected position in AutoCompleteTextView?

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.

How to get selected item from AutoCompleteTextView in android?

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.

What is auto complete text view?

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.


3 Answers

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
           }
    });
like image 105
Shashank Kadne Avatar answered Oct 23 '22 17:10

Shashank Kadne


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 ;)
     }
});
like image 39
SANAT Avatar answered Oct 23 '22 18:10

SANAT


Try AutoCompleteTextView#getListSelection().

like image 2
Sam Avatar answered Oct 23 '22 17:10

Sam