I think TypedArray
is what you are looking for. I have samples using it. If you are interested, take a look at codes below:
First, integer-array
in res/values/arrays.xml
:
<integer-array name="frag_home_ids">
<item>@drawable/frag_home_credit_return_money</item>
<item>@drawable/frag_home_transfer</item>
<item>@drawable/frag_home_balance</item>
<item>@drawable/frag_home_charge</item>
<item>@drawable/frag_home_finance_cdd</item>
<item>@drawable/frag_home_finance_ybjr</item>
<item>@drawable/frag_home_more</item>
</integer-array>
Second, get resource integer values programmatically:
TypedArray tArray = getResources().obtainTypedArray(
R.array.frag_home_ids);
int count = tArray.length();
int[] ids = new int[count];
for (int i = 0; i < ids.length; i++) {
ids[i] = tArray.getResourceId(i, 0);
}
//Recycles the TypedArray, to be re-used by a later caller.
//After calling this function you must not ever touch the typed array again.
tArray.recycle();
Third, call the integer values like this:
holder.iv.setImageResource(ids[position]);
Of course, you can get integer values of string
, color
, integer
, layout
, menu
...in this way.
I hope these codes will inspire you.