1

I have a custom spinner which is replacing my default spinner which was populated by hard coded values in my strings xml.

I have used my custom spinner and populated it dynamically in java but i don't need to do this for this one.

Do i have to populate a list then add it to the spinner in java? and if so how do i populate a list from strings.xml array element?

    List<String> spinnerList = new ArrayList<String>();
    //spinnerList.addAll(R.array.array_spinner);error here, doesnt like this?????????????
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner_custom, spinnerList);
    spinnerSports.setAdapter(adapter);

<resources>
    <string-array name="array_custspinner">
        <item>item1</item>
        <item>item2</item>
    </string-array>
</resources>

1 Answer 1

1

//spinnerList.addAll(R.array.array_spinner);error here, doesnt like this?????????????

yes it is correct. addAll is expecting a Collection of the same type of your List, String in your case. But you are providing an int, the id of array you want to use. Use

List<String> spinnerList = new ArrayList<String>
      (Arrays.asList(getResources().getStringArray(R.array.array_custspinner)));

this way you get a modifiable List<String> which contains all the strings you defined in you array_custspinner

Sign up to request clarification or add additional context in comments.

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.