autocompletetextview setonitemselectedlistener not working
Asked Answered
C

3

37

there i am trying to write code for authorisation activity. When i am putting some entry in inputEmail i expect that my inputPasword will be fileed automaticly if corresponding record exists. However onItemSelectedListener seemd not to work. When i select item nothing happens. Log does not fiers to. I'd like to know if i made some mistake or i am going in wrong aproach.

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

            }

        });
Chlorinate answered 4/6, 2013 at 17:10 Comment(1)
this looks to be a bug on android. On checking the framework code, the callback registered is never used. Maybe you can file a bug reportAllister
J
81

This is a duplicate of this question

However, you need to use AdapterView.OnItemClickListener() not OnItemSelectedListener.

I tested it with success using the following code snippet. Credit to Vogella for the adapter stuff.

    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();
        }
    });
Jutta answered 5/6, 2013 at 12:53 Comment(1)
its giving position of list shown, so position did not match with original data. The real solution here: spapas.github.io/2019/04/05/android-custom-filter-adapter. You need to expose the FilterResult values.Tymbal
L
9
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();
        }
    });

Just get the adapter of AutoCompleteTextView and use the position.

Losing answered 15/2, 2017 at 5:39 Comment(0)
A
0

Use setOnItemClickListener and don't use setOnItemSelectedListener. Also use your custom adapter object to fetch the selected values.

 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());
                }
            });
Arnaldo answered 29/11, 2017 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.