21
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.

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • Do you find a solution? – Accollativo May 18 '14 at 11:32
  • 1
    try taking a look at http://stackoverflow.com/questions/4819813/how-to-get-text-from-autocomplete-textview-android –  Nov 09 '14 at 13:30
  • Solution: http://stackoverflow.com/questions/4819813/how-to-get-text-from-autocomplete-textview-android/37343444#37343444 – Shivang May 20 '16 at 10:03
  • I think the solution for your problem is [here](http://stackoverflow.com/questions/13621762/how-to-find-the-position-of-item-in-a-autocompletetextview-filled-with-array).. check it. – Shanaka Nuwan Aug 24 '16 at 15:36

4 Answers4

6

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
           }
    });
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
  • 12
    Yeah. This is the first thing i tried. But here it gives the **position** of the text selected from the **dropdown**, not the index of the String from the Array i used to populate :( – Archie.bpgc Nov 20 '12 at 06:42
  • Isn't the order remain same? I mean in the Array and the list which comes in the dropdown? – Shashank Kadne Nov 20 '12 at 07:02
  • 1
    But depending on the initial few **Characters** i type in, the dropdown changes (Expected behavior of an AutoCompleteTextView). – Archie.bpgc Nov 20 '12 at 07:04
  • I faced the same problem you have, what I did is a custom adapter where you can set objects instead of strings so you can access to the selected object variables. – user3471194 Feb 07 '17 at 23:12
  • 1
    `String item = parent.getItemAtPosition(pos);` This is the correct answer, Its not `arg1.getItemAtPosition` – Naveen Kumar M Dec 12 '20 at 17:12
3

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 ;)
     }
});
SANAT
  • 8,489
  • 55
  • 66
2

Try AutoCompleteTextView#getListSelection().

Sam
  • 86,580
  • 20
  • 181
  • 179
  • 18
    This always returns **-1**. Which is the **ListView.INVALID_POSITION** as given in the link provided by you. – Archie.bpgc Nov 20 '12 at 06:48
  • This works while the dropdown list is open, however you are asking a tricky question since the list (and therefor the index) changes depending on the AutoCompleteTextView's input... If `Names` is a `List` then you can use `indexOf()` to find the position of the a string after it has been selected. – Sam Nov 20 '12 at 21:40
  • 4
    I'm also finding that `getListSelection()` is always returning `-1` even when the `AutoCompleteTextView` is visible and something is selected. Am I going crazy? – drmrbrewer Feb 20 '20 at 12:16
1
Item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //// id contains item if from database
                    ItemNoSelected = id;
                }
            });

id is from database we can use it

Vijay
  • 2,021
  • 4
  • 24
  • 33