Spinner On Value Change
Solution 1:
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:
publicclassMyOnItemSelectedListenerimplementsOnItemSelectedListener {
publicvoidonItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
publicvoidonNothingSelected(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.
Solution 2:
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:
publicvoidonItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(arg0.equals(spin0)){
spin1.setClickable(true);
if(spin0.getSelectedItem().equals("India"))
{
ArrayAdapter <String> s1 = newArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s1);
}
elseif(spin0.getSelectedItem().equals("Pakistan"))
{
ArrayAdapter <String> s2 = newArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s2);
}
elseif(spin0.getSelectedItem().equals("China"))
{
ArrayAdapter <String> s3 = newArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s3);
}
}
}
Solution 3:
You should define the onItemSelected() separately for each spinner. Otherwise the code gets executed if anything is selected from either spinners.
newCategory.setOnItemSelectedListener(newOnItemSelectedListener() {
@OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stubStringselCat= newCategory.getItemAtPosition(arg2).toString();
if (selCat != "New")
{
loadSpinnerData(topic);
}
}
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
newTopic.setOnItemSelectedListener(newOnItemSelectedListener() {
@OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
loadSpinnerData()
}
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Post a Comment for "Spinner On Value Change"