There is a much easier way to do this.
Create a spinner, add an id to him.
Spinner spinner = new Spinner(context);
spinner.setId(R.id.defined);
and then add an id to the id.xml file in the Values folder.
like this:
<item name="defined_0" type="id"/>
<item name="defined_1" type="id"/>
for your purpose it will come in handy that the id represented by defined_0 + 1 is the same as the defined_1. So you can add the ids programmatically in your for loop
for (int i = 0; i <5; i + +) {
sp = new Spinner (this);
sp.setId(R.id.defined_0+i);
sp.setAdapter (un_adapter);
sp.setOnItemSelectedListener (this);
}
If you put it like this,
the id defined_1 will be linked to the spinner number 2. =)
public void onItemSelected (AdapterView <?> arg0, View view, int pos, long id) {
if (view.getID == R.id.defined_0){
//whatever is supposed to happen if Spinner 1 is selected/used
}
}