Amorphous
Amorphous

Reputation: 697

Can I store the int IDs of drawable in an XML?

So I'm trying to organize my data into an XML as such:

<hands>
    <punch>
        <id>1</id>
        <move>Reverse Punch</move>
        <jap>Gyaku Zuki</jap>
        <thumb>2130837505</thumb>
    </punch>
</hands>

The data from the XML gets parsed and ends up in a hashmap. I'd like to draw them like so:

thumb.setImageURI(Uri.parse(Main.ROOT + Integer.parseInt(moveList.get(MoveView.KEY_THUMB))));

Is using the ID instead of referencing them via R.drawable.img alright?

Thank you!

Upvotes: 1

Views: 83

Answers (2)

Cuong Huynh
Cuong Huynh

Reputation: 159

You should store the name of image in drawable. In your case you should store "img" and use this code to get android Id in your app:

int id = getResources().getIdentifier(elementName,"drawable", getPackageName());

Upvotes: 1

Szymon
Szymon

Reputation: 43023

You should not store the constants from R class and you should always refer to them by R.drawable.img. The reason for it is that the integer values may change at any time and are not guaranteed to be the same every time you build your app.

Upvotes: 2

Related Questions