I have an array of spinners with the same OnItemSelectedListener but I need to know which spinner was selected (preferably where I can get the index of the array) so I can pass it on as the index of another array.
These are my spinners:
insulSpinners = new Spinner[6];
insulSpinners[0] = (Spinner) v.findViewById(R.id.insulSpin1);
insulSpinners[1] = (Spinner) v.findViewById(R.id.insulSpin2);
insulSpinners[2] = (Spinner) v.findViewById(R.id.insulSpin3);
insulSpinners[3] = (Spinner) v.findViewById(R.id.insulSpin4);
insulSpinners[4] = (Spinner) v.findViewById(R.id.insulSpin5);
insulSpinners[5] = (Spinner) v.findViewById(R.id.WireSpin);
awcSpinners = new Spinner[6];
awcSpinners[0] = (Spinner) v.findViewById(R.id.awcSpin1);
awcSpinners[1] = (Spinner) v.findViewById(R.id.awcSpin2);
awcSpinners[2] = (Spinner) v.findViewById(R.id.awcSpin3);
awcSpinners[3] = (Spinner) v.findViewById(R.id.awcSpin4);
awcSpinners[4] = (Spinner) v.findViewById(R.id.awcSpin5);
awcSpinners[5] = (Spinner) v.findViewById(R.id.awcSpin6);
for (int i = 0; i < 6; i++) {
insulSpinners[i].setOnItemSelectedListener(insuls);
awcSpinners[i].setOnItemSelectedListener(awcs);
}
I need the nth spinner of insulSpinners from the listener so I can apply a function to the nth spinner of awcSpinners. The text that is selected on the insulSpinner is used in the function. Any ideas would be appreciated.
The OnItemSelectedListener has two methods.
onItemSelected(AdapterView<?> parent, View view, int position, long id)
onNothingSelected(AdapterView<?> parent)
Both give you the used spinner as a reference AdapterView<?> parent
.
You can add the index of each spinner as a tag and then you can check the tag. From the tag, you can infer the index of the spinner.
For example
int[] ids = {R.id.insulSpin1, R.id.insulSpin2, ...}
for (int idx = 0; idx < ids.length(); i++) {
Spinner spinner = (Spinner) v.findViewById(ids[idx])
spinner.setTag(idx); // you may need to wrap it in an Integer object
insulSpinners[idx] = spinner
}
// and then in your onItemSelected method callback
void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int tag = (Integer) parent.getTag()
Spinner selectedSpinner = insulSpinners[tag]
// do something...
}