How to set the value of a spinner elegantly

Asked

Viewed 112 times

1

Dear to an activity where we are making a change, I am setting the value of spinner(s) in a gross way

     for(int i = 0; i < clienteAdapter.getCount(); i++) {

            Cliente elemento = (Cliente) clienteAdapter.getItem(i);

            if(elemento.getId() == pedido.getId_cliente()) {
                spCliente.setSelection(i);
                break;
            }
        }

I’m getting the data populating the Sqlite Spinner, would have a more elegant way of doing this?

1 answer

0

If you’re using java8 to compile, you can use filter to search the list with predicates:

        Optional elemento = lista.stream().filter(c -> c.getId() == 1).findFirst();
        if (elemento.isPresent()){
            [...]
        }
  • The problem is that Android(for now) can’t stand it java 8 streams

  • Retrolambda and other compatibility libraries can help you write Java 8 and run wherever you want. I can’t remember what the processor was for stream compatibility, but I think in the retrolambda documentation he quotes someone

  • It seems to me that this boy did the backport job : https://github.com/streamsupport/streamsupport/

  • Ramaral and Jefferson, truth, I had not noticed. Was using Android Studio 3.0 Dev. Preview and testing with an android 7.1.2.

  • That’s why I said "for now". With Android Studio 3 is possible however only for apps with minSdk 24 or higher.

Browser other questions tagged

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