0

In my app I have a spinner in which the user selects a number. I have tried the following, and it works, but it requires a string-array in the values.XML file,which is impractical for me as I have several of these with differing ranges (between 80-100 items long).

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.number_array, android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);

This works, but it requires this;

<string-array name="number_array">
<item>1</item>
.....
</string-array>

So I have created an int array instead -

    int [] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

But creating an array adapter like this doesn't work

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        intArray, android.R.layout.simple_spinner_item);

Is there any way to do this or am I stuck with a lot of long numerical string-arrays in my values.xml file?

jcw
  • 5,132
  • 7
  • 45
  • 54
  • 1
    Did you check this http://stackoverflow.com/a/2879680/603744 – Andro Selva Dec 06 '12 at 07:45
  • 1
    you can also create a String array from your int array. Look here: http://stackoverflow.com/questions/3619850/converting-an-int-array-to-a-string-array – fgeorgiew Dec 06 '12 at 08:11

2 Answers2

1

use below code

  String[] array = {"1", "2","3", "4","5","6","7","8","9","10"};
  ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
  (this,android.R.layout.simple_spinner_item, array);

instead of

  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  intArray, android.R.layout.simple_spinner_item);
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
1

Okay, I solved my problem thanks to the links provided in the comments under that question by first converting my int[] array to a string array using this link

Then I used the answers in this link to set the values of the spinner to a string array

Community
  • 1
  • 1
jcw
  • 5,132
  • 7
  • 45
  • 54