Working with 2 spinner on Android

Asked

Viewed 582 times

0

I’m with two spinners in the same Activity. When I click 1 and select the value, the second needs to show the value related to which the user chose in the first spinner.

Example: If in the first spinner he choose Physiotherapy, the second should display only Physiotherapy doctors.

My code of spinner

 //montando os spinners
    ArrayAdapter spinnerEspecialidade = ArrayAdapter.createFromResource(this,
            R.array.spinnerEspecialidade, android.R.layout.simple_list_item_1);

    ArrayAdapter spinnerMedicos = ArrayAdapter.createFromResource(this,
            R.array.spinnerNomeMedicos, android.R.layout.simple_list_item_1);

    spinnerEspecialidade.setDropDownViewResource(android.R.layout.simple_list_item_1);
    spinnerMedicos.setDropDownViewResource(android.R.layout.simple_list_item_1);

    spinnerEspec.setAdapter(spinnerEspecialidade);
    spinnerMedico.setAdapter(spinnerMedicos);

    spinnerEspec.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            selecaoEspec = i;

            if (selecaoEspec == 0){
                spinnerMedico.setEnabled(false);

            }else if (selecaoEspec == 1){
                Toast.makeText(MainActivity.this, "Espec1", Toast.LENGTH_SHORT).show();
                spinnerMedico.setEnabled(true);

            }else if (selecaoEspec == 2){
                Toast.makeText(MainActivity.this, "Espec2", Toast.LENGTH_SHORT).show();
                spinnerMedico.setEnabled(true);

            }else if (selecaoEspec == 3){
                Toast.makeText(MainActivity.this, "Espec3", Toast.LENGTH_SHORT).show();
                spinnerMedico.setEnabled(true);

            }else if (selecaoEspec == 4){
                Toast.makeText(MainActivity.this, "Espec4", Toast.LENGTH_SHORT).show();
                spinnerMedico.setEnabled(true);
            }
        }

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

        }
    });

    spinnerMedico.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            selecaoMed = i;

            if (selecaoMed == 1){
                Toast.makeText(MainActivity.this, "Nome Medico 1", Toast.LENGTH_SHORT).show();

            }else if (selecaoMed == 2){
                Toast.makeText(MainActivity.this, "Nome Medico 2", Toast.LENGTH_SHORT).show();

            }else if (selecaoMed == 3){
                Toast.makeText(MainActivity.this, "Nome Medico 3", Toast.LENGTH_SHORT).show();

            }else if (selecaoMed == 4){
                Toast.makeText(MainActivity.this, "Nome Medico 4", Toast.LENGTH_SHORT).show();

            }else if (selecaoMed == 5){
                Toast.makeText(MainActivity.this, "Nome Medico 5", Toast.LENGTH_SHORT).show();
            }
        }

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

        }
    });
    //FIM DA MONTAGEM DO SPINNER

In my string XML

  <string-array name="spinnerEspecialidade">
    <item>Escolha a Especialidade</item>
    <item>Espec 1</item>
    <item>Espec 2</item>
    <item>Espec 3</item>
    <item>Espec 4</item>
</string-array>

<string-array name="spinnerNomeMedicos">
    <item>Escolha o Médico</item>
    <item>Nome Medico 1</item>
    <item>Nome Medico 2</item>
    <item>Nome Medico 3</item>
    <item>Nome Medico 4</item>
    <item>Nome Medico 5</item>
</string-array>

In my code it’s working like this: Until I select the Specialty spinner 2 is not enabled. Once the user chooses the Specialty, the spinner 2 is enabled showing all "doctors". It would be in this case that wanted to use a filter.

1 answer

2


From what I understand you want the first spinner to be dynamic with the second, :

spinnerEspec.setOnItemSelectedListener(new 
AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        selecaoEspec = i;

        if (selecaoEspec == 0){
            spinnerMedico.setEnabled(false);
        }
        else if (selecaoEspec == 1){
        spinnerMedico.setEnabled(true);

          List<String> list = new ArrayList<String>();
                list.add("Nome 1");
                list.add("Nome 2");
                list.add("Nome 3");
                list.add("Nome 4");

                ArrayAdapter<String> dataAdapter= new ArrayAdapter<String>(SuaAtividade.this, android.R.layout.simple_spinner_item, list);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter.notifyDataSetChanged();
                spinnerMedico.setAdapter(dataAdapter);


                }
  • Thank you very much gave straight. I implemented what I had already done here and is right now working in Firebase also. Solved my problem was just that.

Browser other questions tagged

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