Spinner won't respond to clicks...even though onItemSelected is called?
My Activity implements OnItemSelected listener for a spinner. It has the interesting problem of firing off the onItemSelected callback when the activity shows. So I used a flag hack to solve it (I hate it, but at this point I just want the app to work).
Strangely enough, even though the callback gets called right at activity start, my actual touch selections don't work. I can touch the list, open it, see the strings from the array adapter, and even touch it to make it dismiss - but the callback is never called.
My code:
public class MyActivity extends Activity implements OnItemSelectedListener {
…
private ArrayList<String> mMyTypes = null;
private ArrayAdapter<String> mMyAdapter = null;
private Spinner mMyTypeSpinner = null;
// hack for spinner
boolean isFirstR开发者_如何转开发unWithSpinner = true;
In onCreate():
mMyTypeSpinner = (Spinner) findViewById(R.id.my_activity_spinner);
mMyTypes = new ArrayList<String>();
mMyTypes.add("Test string");
mMyAdapter = new ArrayAdapter<String>(this, R.layout.custom_spinner_style, mMyTypes);
mMyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mMyTypeSpinner.setAdapter(mMyAdapter);
// spinner listener
mMyTypeSpinner.setOnItemSelectedListener(this);
The callback:
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast
.makeText(ConfirmEditActivity.this, "Selected", Toast.LENGTH_LONG)
.show();
if( isFirstRunWithSpinner ) { isFirstRunWithSpinner = false; return; }
…
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast
.makeText(ConfirmEditActivity.this, "Nothing", Toast.LENGTH_LONG)
.show();
}
The toast is shown right when the activity is shown, but when I select items in the spinner, the spinner dismisses and no toast is displayed again (not to mention the rest of the code in the callback fails to execute).
Any observations?
I really hope this something simple...
The spinner will only respond to ItemSelected events when a new item is selected. If there's only one item showing, you obviously can only reselect this one item in the dropdown list. That's the correct behaviour of the android spinner. onItemSelected will be fired if you have more than 1 item in your list, and if the clicked item is not the currently selected item.
From the android docs:
public abstract void onItemSelected (AdapterView parent, View view, int position, long id)
Callback method to be invoked when an item in this view has been selected. This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item.
Hope i could help.
Have you tried setOnItemClickListener() instead of setOnItemSelectedListener()?
If you have already the first item in the spinner selected and you want to "reselect" it (to fire the onItemSelected() callback) you can re-set the adapter by calling:
mMyTypeSpinner.setAdapter(mMyAdapter);
It may seem like a dull thing to do (since you have already set the adapter in onCreate()), but this little trick did work in my case.
精彩评论