AutoCompleteTextView onItemSelectedListener 不起作用

2022-08-31 17:47:23

我有一个AutoCompleteTextView,并设置了onItemSelectedListener,这不起作用。

你知道问题是什么吗?这是我的活动,如果需要,我还可以提供主.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

        }
    }

答案 1

伙计,你会嘲笑你的错误。它为我工作。您已添加,但未添加 。OnItemSelectedListenerOnItemClickListener

仅当您选择项目或使用轨迹球或向上/向下箭头浏览视图时,您的方法才有效。在字符串中再使用一个以“k”开头的值,比如Karen1。键入“k”并在 Karen 和 Karen1 之间进行选择,您将看到它的工作原理。如果要单击,请添加并覆盖OnItemClickListener

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

答案 2

您必须使用

OnItemClickListener

而不是 OnItemSelectedListener


推荐