0

I have to get an array stored in arrays.xml as ArrayList

String arrayNames[]=MyActivity.this.getResources().getStringArray(R.array.names;
ArrayList<String> namesList=(ArrayList<String>) Arrays.asList(arrayNames));

this cause

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

because cast from List to ArrayList seems deniend due to different implementations of List.

I need to pass ArrayList<String> so I cannot change ArrayList<String> namesList in List<String> namesList.

wondering if is there a better way to convert String in ArrayList<String> without write manually a method that iterates all array Entries and put all them in an ArrayList<String>

3 Answers 3

1

You cannot cast it this way, declare a new ArrayList:

ArrayList<String> namesList= new ArrayList<String>(Arrays.asList(arrayNames)));
Sign up to request clarification or add additional context in comments.

Comments

0

You could write it like this:

ArrayList<String> namesList=new ArrayList<String>(Arrays.asList(arrayNames));

1 Comment

We were typing at the same time. I didn't see your answer.
0

You can try using the TypedArray

Resources resources = mContext.getResources();
TypedArray namesList = resources.obtainTypedArray(R.array.names);

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.