In a Spinner type component how do I Seto it with a data coming from a webservice?

Asked

Viewed 41 times

0

Am I doing it this way would be correct? The problem is that it is not working as expected follows code:

Here I take the data that comes from a list and the other screen and save it in situation:

         situacao = bundle.getString("situacao");

Here I want to set the Spinner component with this option :

        spinnerSituacao.setSelection(Integer.parseInt(situacao));

But isn’t it working right what I do? situation is a String that can be "1" or "2".

I added it to the list to be displayed on Spinner this way:

List<String> listaSituacao = new ArrayList<String>();
    listaSituacao.add("Ativo");
    listaSituacao.add("Desativado");

    ArrayAdapter<String> adapterSituacao = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, listaSituacao);

And to take its value that way:

 spinnerSituacao.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Situacao = String.valueOf(position + 1);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
  • What is the expected form??? It is turning into 1 or 0??

  • I’ll edit young man.....

  • What values are in Spinner?

  • I just want to set an already certain value that comes from a webservice and set in Spinner this value on the edit screen

  • 1

    Try it like this: spinnerSituacao.setSelection( (Integer.parseint(situation)-1 );

  • Worked beautifully ;)

  • I need to think in a simplified way even. I really hope to improve this my way of programming...

  • 1

    This you will pick up with time! It is important to pay attention and try to understand what is happening!!! Debug the application and put some Log, it helps a lot! =)

  • Thanks you really helped me. = *** Okay.

Show 4 more comments
No answers

Browser other questions tagged

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