Spinner on value Change Spinner on value Change android android

Spinner on value Change


From the Hello Spinner tutorial:

Now create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner. Here's what this class should look like:

public class MyOnItemSelectedListener implements OnItemSelectedListener {    public void onItemSelected(AdapterView<?> parent,        View view, int pos, long id) {      Toast.makeText(parent.getContext(), "The planet is " +          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();    }    public void onNothingSelected(AdapterView parent) {      // Do nothing.    }}

The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored. Now the MyOnItemSelectedListener needs to be applied to the Spinner. Go back to the onCreate() method and add the following line to the end: spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

In other words, you need to create an OnItemSelectedListener that modifies the value of the second spinner, and attach it to the first spinner.


You have to put the condition on onItemSelected of very first spinner. By this example you can get value of 2nd spinner depending on 1st spinner:

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {        if(arg0.equals(spin0)){        spin1.setClickable(true);                    if(spin0.getSelectedItem().equals("India"))                    {                        ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);                        s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                        spin1.setAdapter(s1);                    }                    else  if(spin0.getSelectedItem().equals("Pakistan"))                    {                        ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);                        s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                        spin1.setAdapter(s2);                    }                    else  if(spin0.getSelectedItem().equals("China"))                    {                        ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);                        s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                        spin1.setAdapter(s3);                    }        }    }    


You should define the onItemSelected() separately for each spinner. Otherwise the code gets executed if anything is selected from either spinners.

    newCategory.setOnItemSelectedListener(new OnItemSelectedListener() {        @Override        public void onItemSelected(AdapterView<?> arg0, View arg1,                int arg2, long arg3) {            // TODO Auto-generated method stub            String selCat = newCategory.getItemAtPosition(arg2).toString();            if (selCat != "New")            {                loadSpinnerData(topic);            }        }        @Override        public void onNothingSelected(AdapterView<?> arg0) {            // TODO Auto-generated method stub        }    });    newTopic.setOnItemSelectedListener(new OnItemSelectedListener() {        @Override        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,            long arg3) {            // TODO Auto-generated method stub            loadSpinnerData()    }        @Override        public void onNothingSelected(AdapterView<?> arg0) {            // TODO Auto-generated method stub        }    });