Is there a way to define an array in XML in which the elements are resource references? For example (it doesn't work, but it explains what I want):
<integer-array name="actions_images"> <item>@drawable/pencil</item> <item>@drawable/pencil</item> <item>@drawable/pencil</item> <item>@drawable/pencil</item> <item>@drawable/pencil</item> <item>@drawable/pencil</item> </integer-array>
To prevent adding duplicates to an array:Use the indexOf() method to check that the value is not present in the array. The indexOf method returns -1 if the value is not contained in the array. If the condition is met, push the value to the array.
Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.
The standard way to find duplicate elements from an array is by using the HashSet data structure. If you remember, Set abstract data type doesn't allow duplicates. You can take advantage of this property to filter duplicate elements.
To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.
If I understand your question correctly, I think I know a workaround (since accessing them via getResources().getIntArray() doesn't work). (You can read more here, that is the source I took the workaround from.)
TypedArray ar = context.getResources().obtainTypedArray(R.array.my_array); int len = ar.length(); int[] resIds = new int[len]; for (int i = 0; i < len; i++) resIds[i] = ar.getResourceId(i, 0); ar.recycle(); // Do stuff with resolved reference array, resIds[]... for (int i = 0; i < len; i++) Log.v (TAG, "Res Id " + i + " is " + Integer.toHexString(resIds[i]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With