How to take position of the item from an Autocompletetextview (Android)

Asked

Viewed 267 times

0

I need to take the position of the item of an autoCompleteTextView and pass the data to other fields. So far my code is like this:

spinner_produtos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id)
    {
        //etCodigo_produto.setText(lista_produtos.getItem(position).getCodigo_produto().toString());
        codigo_produto = lista_produtos.getItem(pos).getCodigo_produto();

But there is no item selected. If anyone can help, thank you.

  • You need the selected value or position of the selected item in your spinner ?

  • Item position, however is not a spinner, only the nomenclature this spinner, is an autoCompleteTextView

  • Leandro, try using the following function int position = youTextView.getSelectedItemPosition();

  • I tried something like this but it didn’t work int position = autoCompleteTextView_products.getSelectedItemPosition();

1 answer

0


From what I understand, the best solution is to go through the array and find the position of the selected item, like this:

spinner_produtos.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
                String selecao = (String) parent.getItemAtPosition(position);
                int pos = -1;

                for (int i = 0; i < lista_produtos.length; i++) {
                    if (lista_produtos[i].equals(selecao)) {
                        pos = i;
                        break;
                    }
                }
                Toast.makeText(getBaseContext(), "Posição " + pos, Toast.LENGTH_SHORT).show();
            }
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.