zomlaaa95 zomlaaa95 - 2 months ago 13
Android Question

Get resource id from typedarray

I have this file array.xml and I want to get the item value from an array.
How can I do that? I have tried with getInt but that returns 0. All help is welcome.

<resources>
<array name="firstAd">
<item>border_top_id_1v</item>
<item>R.id.dugme_1v</item>
<item>R.id.rent_or_buy_1v</item>
<item>R.id.currency_1v</item>
<item>R.id.price_1v</item>
<item>R.id.name_1v</item>
<item>R.id.address_1v</item>
</array>
</resources>

Answer

First, change each item to @id/object instead of R.id.object, then change the tag from array to integer-array, and move the code to your 'integer.xml' resource file.

integer.xml:

<resources>
<integer-array name="firstAd">
    <item>@id/dugme_1v</item>
    <item>@id/rent_or_buy_1v</item>
    <item>@id/currency_1v</item>
    <item>@id/price_1v</item>
    <item>@id/name_1v</item>
    <item>@id/address_1v</item>
</integer-array>
</resources>

Then, programmatically use a TypedArray, like so:

TypedArray firstAd = getResources().obtainTypedArray(R.array.firstAd);
int resourceId = firstAd.getResourceId(index, defValue);