0

I am trying to create a spinner which gets its values from a double array but i get the error: java.lang.Double[], required int, i have values such as 1.4 or 2.8 which is why i cant use an int array. My code is here below:

Double[] FULL = new Double[]{2.25,2.5,2.8};
adapter = ArrayAdapter.createFromResource(this,FULL,android.R.layout.simple_selectable_list_item);

I have tried converting my double array into an int array but that doesn't seem to work as well, i am relatively new to android so i may be doing the conversion wrong. Can i get some help?

3
  • Int indiacate the resources in Android . Default array adapter only takes String array . So just use quotes for double values .and use ArrayAdapter defined String Array Commented May 20, 2018 at 19:36
  • @ADM I need to get the values after that as a number to add to something, will this still work? Commented May 21, 2018 at 10:47
  • You just accepted the answer below. Which is same what i had suggested previously. And to get Value you need to parse it with Double.parseDouble(value). Commented May 21, 2018 at 10:49

1 Answer 1

1

You are using the ArrayAdapter.createFromResource() wrong. Please, refer to the documentation.

To fix your problem, add your FULL array to an array in values > string.xml

<array name="spinner_val">
    <item>"2.0"</item>
    <item>"3.0"</item>
    <item>"4.0"</item>
    ...
 </array>

Then retrieve them as strings since you are going to display them in a text.

ArrayAdapter.createFromResource (this, 
                getContext().getResources().getStringArray(R.array.spinner_val), 
                R.layout.simple_selectable_list_item);
Sign up to request clarification or add additional context in comments.

1 Comment

I need to get the values after that as a number to add to something, will this still work?

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.