Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer array of resource ids returns 0

I have a set of resource ids stored in an array. This is accessed inside the recycler view to populate an image view. The problem is when I access the array, all values returned is 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. 

The drawables are in the hdpi folder only.

like image 568
Rgfvfk Iff Avatar asked Apr 09 '17 13:04

Rgfvfk Iff


1 Answers

Do this:

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();
like image 167
azizbekian Avatar answered Oct 04 '22 00:10

azizbekian