Home » Android » android – How to fill Spinner with int value

android – How to fill Spinner with int value

Posted by: admin May 14, 2020 Leave a comment

Questions:

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

How to&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!!