0

Hi I am creating a list of spinners dynamically based on a user choice. Here I am also implementing OnItemSelectedListener for each of the spinners, since there are multiple spinners I want to know which spinner's method is currently being accessed. Here's the code,

     for (int i = 0; i < count; i++) {
        ArrayList<String> spinnerArray = new ArrayList<String>();
        spinnerArray.add("one");
        spinnerArray.add("two");
        spinnerArray.add("three");
        spinnerArray.add("four");
        spinnerArray.add("five");

        Spinner spinner = new Spinner(this);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {

                Object item = parent.getItemAtPosition(position);
                Log.d("vij-debug", "selector1 no is " + item);
                Log.d("vij-debug", "selector1 id is"+ view.getId());

                 //medicineArray1[i][1]=(String)item;
                // here I want to access the iteration value i



            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_dropdown_item,
                spinnerArray);
        spinner.setAdapter(spinnerArrayAdapter);

Can anyone suggest a suitable solution?

Vij
  • 85
  • 11

2 Answers2

1

Set the Spinner's tag to i upon creation, then retrieve it from the AdapterView<?> parent parameter in onItemSelected().

Spinner spinner = new Spinner(this);
spinner.setTag(i);
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {
            int spinnerNumber = parent.getTag();
        }
        ...
    }
);
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Mike thanks, this works well, however if the user doesn't change the value of the spinner and wishes the first value to be considered, this onItemSelected method will never be called, do you have any suggestion for that? – Vij Aug 04 '14 at 04:03
  • Then the selected item will be whatever you initialize the Spinner with. – Mike M. Aug 04 '14 at 04:07
  • we don't initialize with anything right? it will be displaying the first name in the array which we used to set up the adapter – Vij Aug 04 '14 at 04:11
  • I'm not sure I understand what you're asking, but the way you're creating the Spinners, if the user doesn't select anything, `getSelectedItemPosition()` will return 0. – Mike M. Aug 04 '14 at 04:21
  • when will u call this getSelectedItemPosition() method? and what instance will be used? do you use any event to trigger this call? – Vij Aug 04 '14 at 04:32
  • I don't know how to answer that. What are you trying to do? Your requirement was "I want to know which spinner's method is currently being accessed." Inside the method, `parent` references the Spinner object receiving the action. – Mike M. Aug 04 '14 at 04:39
  • 1
    Mike, i really appreciate your patience, the above solution solves my initial problem, for the rest I'll figure out how to handle the edge cases. Thanks – Vij Aug 04 '14 at 04:47
  • Sure thing. I'm still not certain what you need, but if a Spinner's selected item is never changed by the user, it's going to be the first one in the list, position 0, if that helps. – Mike M. Aug 04 '14 at 04:52
  • Ok, let me tell you what my problem is, i have a bunch of such spinners in my layout, whenever the item is selected, inside the onitemselectedlistener method, I am adding the item selected to a two dimensional array, using your tag as the index for the array. So this array finally will give me all the chosen values of the spinners, however if the user decides to keep the spinners with their intitial value, this array will not be populated for that particular index... That's all my question was – Vij Aug 04 '14 at 05:18
  • When you create the Array, just initialize all the values as if the user won't choose anything. From your code, it looks like you'd want all the values to be `"one"`. That way, you only need to update it if the user changes a Spinner's selection. – Mike M. Aug 04 '14 at 05:24
0

you could use a List and store your spinner there. in your for loop

//inside for loop
List<Spinner> spinnerList=new ArrayList<Spinner>();
Spinner spinner = new Spinner(this);
spinnerlist.add(spinner);

//end

//outside for loop
for(int i=0;i<spinnerList.size();i++){

setListenerToSpinner(spinnerList.get(i));

}

//end


//function
public void setListenerToSpinner(Spinner spinner){

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {

                Object item = parent.getItemAtPosition(position);
                Log.d("vij-debug", "selector1 no is " + item);
                Log.d("vij-debug", "selector1 id is"+ view.getId());

                 //medicineArray1[i][1]=(String)item;
                // here I want to access the iteration value i

            }

}
Ker p pag
  • 1,568
  • 12
  • 25
  • Hi Ker, how does this solve my problem, i would still not know which spinner's item selected listener is currently being executed right? – Vij Aug 04 '14 at 03:55