How to populate Spinner with int value

When you usually fill in the Spinner, as I did in the past, I usually use the SpinnerAdapter, then there are usually elements in the resources to fill it.

Currently, I have another request, I have user input for int in the code, and I want my counter to fill in numbers up to the number selected by the user. Therefore, if the user enters the number "5", it is stored in the int variable. Then I want Spinner to show 1,2,3,4,5 as options.

I'm really not sure how I approach this.

Thanks, Olya

+6
source share
1 answer

Edited

The following is a basic example of how you would add integers to your counter:

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 to your project in accordance with your logic. Also in your case, you should use an ArrayList of integers, as the amount of user selection seems dynamic. you can create an arraylist and replace it for an Integer array in the above code.

Hope this helps!

+23
source

All Articles