OnItemSelectedListener

OnItemSelectedListener

我有一个AutoCompleteTextView并将onItemSelectedListener设置为它,这是行不通的。

你知道是什么问题吗?
这是我的 Activity ,如果需要,我也可以提供main.xml文件。

   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

        }
    }

最佳答案

杜德,你会为自己的错误而笑。它为我工作。您已添加OnItemSelectedListener而不是OnItemClickListener

你的方法只会工作,如果你选择通过 View 使用轨迹球或向上/向下箭头项目或浏览。 Karen1说,在您的字符串中再使用一个以“k”开头的值。输入“k”,然后在Karen和Karen1之间进行选择。您将看到它起作用。如果要单击,则添加OnItemClickListener并覆盖

public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3)

10-05 18:53