资源ID的整数数组返回0 [英] Integer array of resource ids returns 0
本文介绍了资源ID的整数数组返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组存储在数组中的资源ID。这可以在回收器视图中访问以填充图像视图。问题是当我访问数组时,返回的所有值都是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.
抽奖是在仅限hdpi文件夹。
The drawables are in the hdpi folder only.
推荐答案
这样做:
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();
这篇关于资源ID的整数数组返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文