Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 325
Oli Black Avatar asked Jan 09 '13 01:01

Oli Black


People also ask

What is spinner in mad?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.


1 Answers

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!!

like image 92
Abhishek Sabbarwal Avatar answered Nov 10 '22 15:11

Abhishek Sabbarwal