I want to catch user interaction with a spinner like onCLickListener. Do to the 'don't call onClickListener() on AdapterView' error I found recommendations that you should override a constructer with a custom spinner to set onClickListener() on the view the spinner creates.
Tried that:
public class MySpinner extends Spinner {
public static final String TAG = "MyApp";
public MySpinner(Context context, AttributeSet attrs) {
super(context, attrs);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.temp_systems, android.R.layout.simple_spinner_item);
TextView spinner_text = (TextView) findViewById(android.R.id.text1);
OnClickListener spinnerOnClickL开发者_如何转开发istener = new OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "Should do something!");
}
};
spinner_text.setOnClickListener(spinnerOnClickListener);
setAdapter(adapter);
}
}
but when I try to include this in a layout I get a crash on failing to inflate this item.
To clarify here, onItemClickListener fires when the user clicks an item in the dropdown menu, not when the spinner is collapsed. I need to intercept after the initial spinner is clicked but before it creates the dropdown menu.
What they might mean is to Override
the setOnItemClickListener
and then call that in the constructor. So in your mySpinner
class you would need to add: [notice it is called on ITEM click listener, that might also be part of your issue]
@Override
public void setOnItemClickListener(
android.widget.AdapterView.OnItemClickListener l) {
super.setOnItemClickListener(l);
//... do action here that you want to happen when item in spinner is clicked
}
Dunno if that will fix your issue but I hope it helps. Good Luck.
also it might be worthwhile to possibly use
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//do stuff
}
Not sure why you are needing to extend Spinnner or why you are assigning a click listener on the TextView. You should be assigning a selection listener on the spinner. As per the Spiner example consider:
1) Having a layout with a Spinner control
2) Having a 'Spinner' member of your activity
3) Inflate the layout via setContentView
and then assign the spinner member via findViewById
4) Set the adapter for the spinner and call setOnItemSelectedListener
on your spinner to assign a selection listener
You can use OnTouchListener instead of OnClickListener, which is not available for Spinner.
精彩评论