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?

2

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.