Reputation: 239
I have 2 spinners in an Activity, which take their data from the same Resource (xml string array). Code is like this:
Spinner spinnerFrom = (Spinner) findViewById(R.id.spinner_from_to);
Spinner spinnerTo = (Spinner) findViewById(R.id.spinner_from_to);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.length_from_to, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerFrom.setAdapter(adapter);
spinnerTo.setAdapter(adapter);
But now in the
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
}
method, I get the correct values fom the spinners when I test it e.g. with a toast, but couldn't manage to know, from what of these two spinners the value comes from. I tried it with different parent-methods, but didn't help.
May you give me a hint, please?
Thanks much in forward.
Martin
Upvotes: 0
Views: 143
Reputation: 2814
You can add an android:tag
value to each spinner in the xml. Then use view.getTag()
to in the onItemSelected()
to get tag, enabling you to see from which one it was called.
Upvotes: 3
Reputation: 45
You seem to have same id for both the spinners R.id.spinner_from_to
.
Anyway... In order to get id of the spinner you can use
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
int id = parent.getId();
}
Upvotes: 1
Reputation: 29285
You could simply use view.getId()
.
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
int id = parent.getId();
}
And with regard to the ID you've gotten decide what each item should do.
Upvotes: 1