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

Archie.bpgc picture Archie.bpgc · Nov 20, 2012 · Viewed 20.1k times · Source
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.

Answer

Shashank Kadne picture Shashank Kadne · Nov 20, 2012

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
           }
    });