OnItemSelectedListener

OnItemSelectedListener

在那里,我正在尝试编写用于授权 Activity 的代码。
当我在inputEmail中输入一些条目时,如果存在相应的记录,我希望我的inputPasword会自动归档。但是onItemSelectedListener似乎不起作用。当我选择项目时,没有任何反应。日志不正确。
我想知道我是否犯了一些错误,或者我走错了路。

AutoCompleteTextView inputEmail;
inputEmail = (AutoCompleteTextView) findViewById(R.id.loginEmail);
inputEmail.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Set<String> keys = prefs.getAll().keySet();
                emails.clear();

                if (keys.size() != 0) {
                    emails.addAll(keys);
                } else
                    emails.add("");

                inputEmail.setAdapter(adapter);

                email = inputEmail.getText().toString();

            }

        });
inputEmail.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                email = inputEmail.getText().toString();
                password = prefs.getString(email, "");
                Log.d(email + " "+password, "email+password");
                if (password.length() > 1) {
                    inputPassword.setText(password);
                }

            }

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

            }

        });

最佳答案

这是此question的副本

但是,您需要使用AdapterView.OnItemClickListener()而不是OnItemSelectedListener

我使用以下代码段成功进行了测试。记下Vogella作为适配器的资料。

    AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autocomplete_textview);

    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
            "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
            "Android", "iPhone", "WindowsMobile" };

    ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
        list.add(values[i]);
    }
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, list);
    actv.setAdapter(adapter);

    actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(MainActivity.this,
                    adapter.getItem(position).toString(),
                    Toast.LENGTH_SHORT).show();
        }
    });

07-27 18:45