How can I change a Spinner according to another Spinner?

0

I have the following interface, when selecting a brand you must put the models of that brand, but I do not know how to change the items of the models according to the selected brand

    
asked by Maria 26.05.2018 в 05:55
source

1 answer

0

For this the spinner brings with it a method that will be listening every time a change happens: setOnItemSelectedListener . It is very easy to use and understand.

I attached the example I generated for your question.

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

               //Sirve para obtener el valor del spinner 
                String marca = spinner.getItemAtPosition(position).toString(); 

                if(marca.equals("Volkswagen")){
                    array2 = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, lista2); 
                    spinner2.setAdapter(array2);

                }   else if(/*La otra marca*/){
                     //Cargar otra lista de modelos de la marca seleccionada
                }                 
            }

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

            }
        });

The onItemSelected will hear which brand was selected and will proceed to show the results in another spinner. That would be it.

Greetings.

    
answered by 26.05.2018 в 07:17