Reputation: 39
I'm drawing some spinners on fly. The question is, how I can to know that I'm using in onItemSelected.
Example:
for (int i = 0; i <5; i + +) {
sp = new Spinner (this);
sp.setAdapter (un_adapter);
sp.setOnItemSelectedListener (this);
}
What spinner I'm using?
public void onItemSelected (AdapterView <?> arg0, View view, int pos, long id) {
//??????????????
}
Upvotes: 0
Views: 78
Reputation: 118
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
}
}
Upvotes: 0
Reputation: 2528
you can use setTag and getTag to identify current spinner. i.e
for (int i = 0; i <5; i + +) {
sp = new Spinner (this);
sp.setAdapter (un_adapter);
sp.setTag(i.toString());
sp.setOnItemSelectedListener (this);
}
and can get it like
public void onItemSelected(AdapterView<?> arg0, View v, int p,
long arg3) {
arg0.getTag;
}
Upvotes: 1