Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App The Model-View-Presenter Pattern Adding Story Resources

Alan Kuo
Alan Kuo
7,697 Points

I don't understand why we use int variable to get textId and other ids since the value and the id are not integers?

So in the videos, we're using variable int textId to get values from strings.xml, but the both the id and the value are not integers, I don't really understand why we're using int instead of String properties.

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

The string resources (and other resources) get integer IDs assigned at compile time. That is where the ints come from. We conveniently use string names in the resource xml files to make it more readable for us. So when we do:

new Choice(R.string.page0_choice1, 1)

The R.string.page0_choice1 value actually becomes an integer, just like we defined in private int textId;

Here's a more detailed answer: https://stackoverflow.com/questions/6517151/how-does-the-mapping-between-android-resources-and-resources-id-work

Hope that helps!