Click here to Skip to main content
15,921,212 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I made 3 spinners and what i want is when user selects spinner 1, spinner 2 and 3 change their value accordingly to spinner 1.

I have one little problem. I made, when spinner 1 value is changed spinner 2 is changing too, but my third spinner wont change value

CODE:

JavaScript
  Spinner grad,skola,zanimanje;

String gradovi[]={"Banja Luka","Bijeljina"};
String blskola[]={"JU Gimnazija","Zastiti Me","Skola 3"};
String blsmjer[]={"Opšti","MUZAFFARPUR","GAYA"};
String bijeljinaskole[]={"Skola 1","Skola 2","Skola 3"};
String bijeljinasmjer[]={"Smjer 1 Bijeljina","Smjer 2","Smjer 3"};



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pocetni);

    grad = (Spinner) findViewById(R.id.spinnergrad);
    skola = (Spinner) findViewById(R.id.spinnerskola);
    zanimanje = (Spinner) findViewById(R.id.spinnerzanimanje);

    final ArrayAdapter<String> gradovispinner = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, gradovi);

    final ArrayAdapter<String> banjalukaskola = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, blskola);

    final ArrayAdapter<String> banjalukasmjer = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, blsmjer);

    final ArrayAdapter<String> bijeljinas = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bijeljinaskole);

    final ArrayAdapter<String> bijeljinasmj = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, bijeljinasmjer);


    gradovispinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    grad.setAdapter(gradovispinner);
    skola.setAdapter(banjalukaskola);



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

            switch (parent.getId()) {
                case R.id.spinnergrad: {
                    if (gradovi[position].equals("Banja Luka")) {


                        banjalukaskola.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        skola.setAdapter(banjalukaskola);

                        banjalukasmjer.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        zanimanje.setAdapter(banjalukasmjer);


                    }
                    else if(gradovi[position].equals("Bijeljina")){

                        bijeljinas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        skola.setAdapter(bijeljinas);

                    }

                }

            }

        }

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

        }
    });
}


What I have tried:

Everything is up there in code
Posted
Updated 26-Apr-18 7:22am

1 solution

Your if() statement should probably be:
if (gradovi[position].equals("Banja Luka"))
{
    banjalukaskola.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    skola.setAdapter(banjalukaskola);

    banjalukasmjer.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    zanimanje.setAdapter(banjalukasmjer);
}
else if(gradovi[position].equals("Bijeljina"))
{
    bijeljinas.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    skola.setAdapter(bijeljinas);

    bijeljinasmj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    zanimanje.setAdapter(bijeljinasmj);
}


Sorry for the ill-formatting. The <pre> tags would not play nicely with those long statements.
 
Share this answer
 
Comments
Member 13800007 26-Apr-18 13:52pm    
Thanks a lot,it was that easy oh Im so stupid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900