Reputation: 4763
this may be a simple question but i was wondering, i have an application that has two spinners and a textview which displays a boolean. the two spinners have numbers between 1 and 10 and i want the boolean to display true when ever the numbers from the two spinners equal to 12. i have all the code to do this and to check are the numbers in the spinners = 12 but i dont know where to put the code to check are the numbers = 12 when ever one of the spinners are changed
so long story short, is there a onClick command i can use to call this code when ever someone clicks on a spinner and changes the number in the spinner?
Thanks
Upvotes: 0
Views: 2625
Reputation: 7082
You have to use an on item selected listener:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Here you check the spinner values sum
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Upvotes: 1
Reputation: 40002
Get an instance of the spinner and add a listener for when an item is selected. That way it will trigger after the user has clicked the spinner, and selected a value.
Spinner s = (Spinner)findViewById(R.id.myspinner);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 2
Reputation: 12823
onItemSelected()
http://developer.android.com/resources/tutorials/views/hello-spinner.html
Upvotes: 0