Oli Black
Oli Black

Reputation: 441

How to fill Spinner with int value

When normally populating a Spinner as I have done in the past I normally use a SpinnerAdapter then normally have items in resources to populate it.

I have currently though a different query, I have in my code a user input for an int and I want my spinner to populate with numbers up to the user selected number. So if the user enters the number '5' it is saved to an int variable. I then want the Spinner to show 1,2,3,4,5 as choices.

I am really not sure how I would approach this.

Thanks, Oli

Upvotes: 11

Views: 28211

Answers (1)

Abhishek Sabbarwal
Abhishek Sabbarwal

Reputation: 3818

Edited

Below is a basic example of how you would add Integers to your spinner :

mspin=(Spinner) findViewById(R.id.spinner1);
Integer[] items = new Integer[]{1,2,3,4};
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
mspin.setAdapter(adapter);

You can refer to this and make changes in your project as per your logic. Also in your case you should use an ArrayList of integers since the number of choice of the user seems to be dynamic. you can create an arraylist and replace in for the Integer array in the above code.

Hope this helps!!

Upvotes: 31

Related Questions