Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

java - AutoCompleteTextView onItemSelectedListener does not work

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

        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...