An array of resource identifiers returns 0

I have a set of resource identifiers stored in an array. To access the viewing image in the recycler view, access to it. The problem is that when I access the array, all the return values ​​are 0.

// arrays.xml
<array name="array_category_icons">
    <item>@drawable/autumn</item>
    <item>@drawable/backpack</item>
</array>

// inside recycler view adapter
int[] myIcons = getActivity().getResources().getIntArray(R.array.array_category_icons);

myIcons[i] always returns 0. 

Only displayed in the hdpi folder.

+4
source share
1 answer

Do it:

TypedArray ta = getResources().obtainTypedArray(R.array.array_category_icons);
Drawable[] icons = new Drawable[ta.length()];
for (int i = 0; i < ta.length(); i++) {
    int id = ta.getResourceId(i, 0);
    if (id != 0) {
        icons[i] = ContextCompat.getDrawable(this, id);
    }
}
ta.recycle();
+5
source

Source: https://habr.com/ru/post/1674422/


All Articles