36

我正在尝试为授权活动编写代码。当我在 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

            }

        });
4

3 回答 3

74

这是这个问题的副本

但是,您需要使用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();
        }
    });
于 2013-06-05T12:53:02.310 回答
6
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String selectedItem=autoCompleteTextView.getAdapter().getItem(position).toString();
            Toast.makeText(getApplicationContext(),selectedItem ,  Toast.LENGTH_SHORT).show();
        }
    });

只需获取 AutoCompleteTextView 的适配器并使用该位置即可。

于 2017-02-15T05:39:20.757 回答
0

使用 setOnItemClickListener,不要使用 setOnItemSelectedListener。还可以使用您的自定义适配器对象来获取选定的值。

 prodNameAutoCompTxt.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    //Don't use this method
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                    System.out.println("Nothing selected");
                }
            });
            prodNameAutoCompTxt.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    //Use this method
                    Product prodObj=(Product) prodAutoAdapter.getItem(i);
                    new UomSynch().execute(prodObj.getProdID());
                }
            });
于 2017-11-29T09:29:51.627 回答