[英]Integer array of resource ids returns 0
I have a set of resource ids stored in an array. 我有一组存储在数组中的资源ID。 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.
问题是当我访问数组时,返回的所有值都是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. drawable只在hdpi文件夹中。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.