Working with 2 spinner on Android

0

I have two spinners in the same Activity . When I click on 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%% of% he chooses Physiotherapy, the second should only display Physiotherapy Physician.

My code for 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 XML strings

  <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 works like this: While I do not select the Specialty, spinner 2 is not enabled. Once the user chooses the Specialty, spinner 2 is enabled showing all "doctors". It would then be that I wanted to use a filter.

    
asked by anonymous 02.08.2017 / 23:39

1 answer

2

From what I understand you want the first spinner to be dynamic with the second, I believe that this will work:

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);


                }
    
03.08.2017 / 19:42