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

Jose Nunez
Jose Nunez
923 Points

Understanding how we constructed the pages in the Constructor

hey guys so i'm a little confused with a certain part of the code.

this is the constructor that we have in our Page class

public Page(int imageId, int textId, Choice choice1, Choice choice2) { this.imageId = imageId; this.textId = textId; this.choice1 = choice1; this.choice2 = choice2; }

it says the constructor takes in 2 integers and 2 Choice objects, cool i understand that. but when call the constructor from our Story class, we put something like this

pages[0] = new Page(R.drawable.page0, R.string.page0, new Choice(R.string.page0_choice1,1), new Choice(R.string.page0_choice2,2));

i dont understand why we are passing a string to the constructor if its expecting an int, or does (R.string.page0) sends an int instead of a string

3 Answers

that's true, resource in android is represented as integer value so R.string.page0 could be something like this 17864 Everything in the R class is a reference and represented as int value

Alan Kuo
Alan Kuo
7,697 Points

I asked the same question on Stack Over Flow and got the answer below.

int variables can hold not just numbers but other values like in the case.

If you go check out R.string.page0 you will also find it restored as an int variable.

all resources in android studio are strings but they convert into ints in compile time, thank you