Reputation: 623
I have to know the click event from the spinner in the Activity. The OnClick Method of the MultiSelectionSpinner Class gets called, but I have no idea how to create a callback method for the Activity. Following is the Spinner Class and the Activity.
public class MultiSelectionSpinner extends Spinner implements
OnMultiChoiceClickListener {
String[] _items = null;
boolean[] mSelection = null;
ArrayAdapter<String> simple_adapter;
public MultiSelectionSpinner(Context context) {
super(context);
simple_adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
public MultiSelectionSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
simple_adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (mSelection != null && which < mSelection.length) {
mSelection[which] = isChecked;
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
} else {
throw new IllegalArgumentException(
"Argument 'which' is out of bounds.");
}
}
public class mainActivity extends FragmentActivity
// Thats the spinner
MultiSelectionSpinner spinner;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Here I load the spinner with the Names
spinner = (MultiSelectionSpinner) findViewById(R.id.spinner);
spinner.setItems(Names);
Upvotes: 0
Views: 76
Reputation: 9070
The most straightforward way is to create another listener interface in MultiSelectionSpinner
which your Activity implements.
public interface MySpinnerListener{
public void onItemClicked(int which);
}
MySpinnerListener listener;
public void setListener(MySpinnerListener listener) {
this.listener = listener;
}
public void onClick(int which,...){
listener.onClick(which);
...
}
Then implement the callback in your Activity
spinner = (MultiSelectionSpinner) findViewById(R.id.spinner);
spinner.setListener(new MultiSelectionSpinner.MySpinnerListener(){
@Override
public void onClick(int which){
// callback
}
});
Upvotes: 1