This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Finally, we will make our model code even easier to work with by adding some custom constructors to our Page and Choice classes.
GitHub Repo
Code for copy/paste:
pages[1] = new Page(R.drawable.page1,
R.string.page1,
new Choice(R.string.page1_choice1, 3),
new Choice(R.string.page1_choice2, 4));
pages[2] = new Page(R.drawable.page2,
R.string.page2,
new Choice(R.string.page2_choice1, 4),
new Choice(R.string.page2_choice2, 6));
pages[3] = new Page(R.drawable.page3,
R.string.page3,
new Choice(R.string.page3_choice1, 4),
new Choice(R.string.page3_choice2, 5));
pages[4] = new Page(R.drawable.page4,
R.string.page4,
new Choice(R.string.page4_choice1, 5),
new Choice(R.string.page4_choice2, 6));
pages[5] = new Page(R.drawable.page5, R.string.page5);
pages[6] = new Page(R.drawable.page6, R.string.page6);
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We now want to add some custom
constructors to our model objects.
0:00
Let's open up our page class first.
0:04
Now, we could just type out
a constructor here, but once again,
0:06
there's a really helpful
shortcut from Android Studio.
0:09
If we click on Code, and then Generate,
we can pick a Constructor.
0:12
And we want to use all four of these as
parameters, so I'll hold down Shift and
0:16
click on the last item.
0:20
Click OK and there we go, we have a nice
tidy constructor that will populate a new
0:22
page object with all four settings.
0:27
Let's do the same thing for
our Choice class.
0:29
So again, I'm gonna put my cursor where
I want the new constructor to go,
0:31
then click on Code > Generate >
Constructor, select both and click OK.
0:35
All right, so
let's use these back in our story class.
0:41
Let's get rid of this code here, and
0:44
we'll do it a little bit differently
with our fancy new constructors.
0:46
So, this time the page at
index 0 will equal a new Page,
0:50
and we can pass in those parameters we
were all ready using, R.drawable.page0.
0:58
For the text ID, R.string.page0.
1:05
And for the choices, we can create
new choice objects right in line.
1:09
But just to make this
a little easier to read,
1:14
I'm going to put each
parameter on a new line.
1:15
So next, we have choice one,
so I'll say newChoice.
1:19
And the text will be
R.string.page0_choice1.
1:23
The second primer is the next page,
but I'm gonna leave that blank for
1:30
just a second.
1:33
Add a comma and do it again,
new Choice R.string.page0_choice2.
1:35
With another blank.
1:41
I will add this semicolon though.
1:43
Right now we're working on Page 0, here.
1:45
The first choice takes us to page 1, and
the second choice takes us to page 2, so
1:47
we can plug in the page
numbers in our constructor.
1:52
Okay, so here's the first choice,
and it goes to page 1, and
1:55
the second choice goes to page 2.
1:58
Cool, we have our first page.
1:59
The rest will follow this same format,
so I'm just going to copy and
2:02
paste the remaining page details.
2:05
This code is once again available for
you to copy and
2:07
paste from the teacher's notes.
2:10
Now, I have some errors down here
at the bottom, but that's okay.
2:12
Take a look at pages 5 and
6 in our story map.
2:15
These are the two final
pages of the story,
2:18
which means that we don't have
any choices at this point.
2:21
How should we handle this scenario?
2:23
Once again,
we could solve this a few different ways.
2:25
But since we don't need any choices, let's
add a second custom constructor in our
2:27
page class that simply doesn't
have any choice parameters.
2:32
So back here I'm gonna add a new one
up here and I'm going to generate it.
2:35
Code > Generate a constructor, and
this time I only want image and text.
2:41
So, without setting any choice parameters,
those will now be null by default.
2:47
Now, for
this scenario where it is a final page,
2:52
I want to add one more
variable that tells us so.
2:55
So, here at the top,
lets add private boolean isFinalPage.
2:58
IsFinalPage will be false by default, but
3:06
I'd like to make that explicitly clear
by adding = false just to make sure.
3:08
Now, inside of our new constructor,
3:13
we can add another line to
set final page to true.
3:16
So this.isFinalPage in this case is true.
3:20
We'll use this when we're crafting
our story in just a little bit.
3:23
We should add getters and setters for
this new variable too, so
3:26
I'm going to add a new line down here.
3:29
Then click on Code and Generate >
Getter and Setter for the new variable.
3:31
Click OK and there we go.
3:36
Our data model is complete and
we have a story to read.
3:39
In the past few videos, we explored how
to turn the abstract data of this app
3:41
into a set of concrete Java
objects that we could use.
3:45
The way we structured our
objects allows us to store and
3:48
manage the data separately from the view
and controller parts of our app.
3:51
Which is a core component
of the MVC pattern.
3:55
Now, I don't know about you but
3:57
I am really excited to finally
display our story in the app.
3:59
So, why don't you take a break for
4:01
a co-challenge, and then we will
finish it up in the next section.
4:02
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up