Using Spinner on Android

1

I'm developing an Android application, and in it I have a Spinner and from the choice the user makes in that Spinner it goes to a certain page.

I had done so:

//Acionando_Items_no_Spinner
public void addListenerOnSpinnerItemSelection() 
{
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public void addListenerOnButton() 
{
    spinner1 = (Spinner) findViewById(R.id.spinner1);//Informação_Do_Spinner
    button = (Button) findViewById(R.id.button);//Informação_Do_Botão
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Escolha_da_pagina
            if(String.valueOf("São Paulo") != null){  
                setContentView(R.layout.activity_page2);
            }else{
                if(String.valueOf("Rio de Janeiro") != null){  
                    setContentView(R.layout.activity_page3);
                }
            }

        }
    });
}

In the beginning it worked, but when it has more than one option, as shown in the code above, it goes into the first if and even if it is wrong it gives the result of the first if .

    
asked by anonymous 19.06.2014 / 00:42

1 answer

3

The fact that it always enters the first if is that String.valueOf is always different from null .

I believe that what you want to do is if in relation to the Spinner item and for this you need to make the following change:

public void addListenerOnButton() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);//Informação_Do_Spinner
    button = (Button) findViewById(R.id.button);//Informação_Do_Botão

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String valorDoSpinner = spinner1..getSelectedItem().toString();
            //Escolha_da_pagina
            if(valorDoSpinner.equals("São Paulo")){  
                setContentView(R.layout.activity_page2);
            }else if(valorDoSpinner.equals("Rio de Janeiro")){  
                setContentView(R.layout.activity_page3);
            }
        }
    });
}

In addition there is another problem not mentioned, you can not call setContentView after you have already called in onCreate . You need to start another Activity using Intent or start using Fragments .

    
19.06.2014 / 00:51