57

I have an AutoCompleteTextView and set onItemSelectedListener to it, which does not work.

Have you any idea what is the problem? Here is my Activity, I can also provide the main.xml file if it is needed.

   package com.chidem;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;

    public class ChidemActivity extends Activity implements OnItemSelectedListener{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            String hop[]=new String[]{
                    "Karen","Mika","Gevor"
            };

            AutoCompleteTextView searchHotels = (AutoCompleteTextView) findViewById(R.id.autoSearch);
            searchHotels.setOnItemSelectedListener(this);

            ArrayAdapter<String> adapter1 = new ArrayAdapter<String>( this, R.layout.list_item, hop);
            searchHotels.setAdapter(adapter1);

        }


        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Log.d("autocomplete", "itemselected");

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    }
Janusz
  • 187,060
  • 113
  • 301
  • 369
Karen
  • 1,025
  • 1
  • 10
  • 22

4 Answers4

117

Dude, you will laugh at your mistake. Its working for me. You have added OnItemSelectedListener and not OnItemClickListener.

Your method will only work if you select your items or browse through the view using a trackball or up/down arrows. Use one more value in your String starting with "k" say Karen1. Type "k" and select between Karen and Karen1.You will see that it works. If you want click, then add OnItemClickListener and override

public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3)
Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
  • 2
    You should select this as the right answer..so that other people viewing this link would come ti know what the problem was? – Shashank Kadne Feb 13 '12 at 18:27
  • 2
    I am laughing too, stupid mistake, but as we see, a lot of people are confused by that. – Renetik Aug 23 '20 at 17:34
  • There's nothing to laugh at here. This isn't a mistake on the developers' end, but on Google's end. What's the point of providing such a method when it does not work in first place? A lot of developer are migrating from `Spinner`, for example, who are used to using `AdapterView.OnItemSelectedListener` which is also provided here but does not work. – Muntashir Akon Jul 01 '23 at 13:30
  • This does NOT work!? – Learn2Code Jul 16 '23 at 16:59
16

You must use

OnItemClickListener

instead of OnItemSelectedListener

user1337052
  • 181
  • 1
  • 4
6

Looks like, of-late, the real problem may not be in the code of the question asked, but in the source code of the AutoCompleteTextView class itself.

Although the AutoCompleteTextView class sets it's OnItemSelectedListener through the setOnItemSelectedListener method, but nowhere in the class any listener method is called. Hence the listener remains unused. (reason best known to developers!)

Source code here: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/AutoCompleteTextView.java

As such, the alternative is to use the OnItemClickListener.

Himanshu Likhyani
  • 4,490
  • 1
  • 33
  • 33
  • Well... I don't know if you have saw this but the setOnItemSelectedListener method is deprecated. – Iogui Feb 06 '21 at 00:30
0

For JAVA

myAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                
                Log.e(tag, "Clicked!");
                myAutoCompleteTextView.clearFocus();
            }
        });
XpressGeek
  • 2,889
  • 3
  • 23
  • 27