Assign an intent to each item of a spinner

0

Good, I have a spinner with the following values: Teacher and Student, my question is, how do I do it so that if the person chose Professor of the spinner direct him to a activity , but if you chose Student from spinner address another activity , so that my question is more understandable I leave my code

    Spinner tipuus;
    String[] tipos={"Elija una opción","Alumno","Profesor"};
    //---------------------------
    tipuus= (Spinner) findViewById(R.id.spntipusu);
    //---------------------------
    ArrayAdapter<String> listatipusu=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,tipos);
    tipuus.setAdapter(listatipusu);
    //---------------------------
    btnregistrar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (tipuus.equals("Alumno")){
                         Toast.makeText(LoginActivity.this, "Usted a escogido alumno", Toast.LENGTH_SHORT).show();
                    }if (tipuus.equals("Profesor")){
                         Toast.makeText(LoginActivity.this, "Usted a escogido profesor", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(LoginActivity.this, "Usted no ha escogido un tipo de usuario", Toast.LENGTH_SHORT).show();
                    }
                }
            });

I know that the tipuus.equals("Alumno") part is wrong, I would like to know what the code would be like to do what I ask. Thanks

    
asked by kevvelas 14.11.2017 в 06:04
source

1 answer

1

To obtain the selected item within your spinner usas:

tipuus.getSelectedItem().toString().equals("alumno") // dentro del "" debe ir un String que exista en tu arreglo. es este caso alumno o profesor.

Then you must add an intent within a condition if :

Intent intent = new Intent(nombre_actividad_actual.this,nombre_actividad_destino.class);
                startActivity(intent);
                nombre_activdad_actual.this.finish();

It would stay like this:

 btnregistrar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (tipuus.getSelectedItem().toString().equals("alumno"))){
                             Intent intent = new Intent(nombre_actividad_actual.this,alumno.class);
                startActivity(intent);
                nombre_activdad_actual.this.finish();

                    } else if (tipuus.getSelectedItem().toString().equals("profesor")){
Intent intent = new Intent(nombre_actividad_actual.this,´profesor.class);
                startActivity(intent);
                nombre_activdad_actual.this.finish();

                    }else {
                        Toast.makeText(LoginActivity.this, "Usted no ha escogido un tipo de usuario", Toast.LENGTH_SHORT).show();
                    }
                }
            });

As a note, you must differentiate the use of the if and the else if . the if evaluates all the conditions and the else if when finding one that is true stops and executes the action that is inside it. Help optimize your app.

    
answered by 14.11.2017 в 07:55