Reputation: 12395
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>
Upvotes: 0
Views: 435
Reputation: 9
You can try using the TypedArray
Resources resources = mContext.getResources();
TypedArray namesList = resources.obtainTypedArray(R.array.names);
Upvotes: 0
Reputation: 8386
You could write it like this:
ArrayList<String> namesList=new ArrayList<String>(Arrays.asList(arrayNames));
Upvotes: 0
Reputation: 13761
You cannot cast it this way, declare a new ArrayList
:
ArrayList<String> namesList= new ArrayList<String>(Arrays.asList(arrayNames)));
Upvotes: 1