This course will be retired on June 1, 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
Start a free Courses trial
to watch this video
In this video we will start using our new model classes to represent an entire story within the context of our app.
GitHub Repo
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 had a lovely page class, but
0:00
how should we cover individual page
objects together into a cohesive story?
0:02
As usual, there are lots of
ways we could approach this.
0:08
We saw at an earlier project how to
use arrays to hold a set of data, so
0:10
let's use that again.
0:14
We'll create an array of page objects and
0:15
then we'll load our actual
story data into each page.
0:17
Then we can pull pages out of
the array as needed to display them
0:20
in our story activity.
0:23
Let's go back to story activity and
0:26
start with what seems like it
might be an obvious choice.
0:28
Let's add a new private array
of page objects called pages.
0:32
Now I've got an error about trying
to import the page class, so
0:38
I'll hit Alt + Enter and
I want to make sure we get the one
0:41
from our model package, not the PDF
document or renderer down there.
0:44
Now we need to add our
story data to the array.
0:50
So what should I do here?
0:52
How about we initialize our
array down here and uncreate.
0:55
So let's say pages will equal a new array,
page objects of size seven.
0:58
So at this point we can start
adding page details on at a time,
1:06
but this already doesn't feel quite right.
1:09
Remember how I said that
one of the goals of MVP
1:12
is to keep data details
in their own objects.
1:15
If we add all this data in here,
then we're starting to blur those lines.
1:18
This activity, this presenter
object will know about the data.
1:21
Instead, let's add another class
called Story that will create and
1:25
hold this array of pages for us.
1:29
So let's get rid of these
lines we added here.
1:31
And up at the top,
we'll delete this member variable.
1:34
Then we can right-click on our model,
select new Java class and call this Story.
1:37
So we still want to do the same thing.
1:44
We want a private array
of pages called Pages.
1:45
And now we need a constructor
to set up our story.
1:52
So let's type public Story and
inside of it,
1:55
we can say Pages will equal a new
array of pages of size seven.
2:00
So why aren't we putting
this right in our activity?
2:05
We technically could do that and
it would work fine.
2:08
But if we spend a little time and
effort making things more organized and
2:10
following best practices like this.
2:14
Then our code would be easier
to maintain and easier for
2:16
others to learn if they need
to work on it down the road.
2:19
And even easier for
2:21
ourselves when we come back to it later
after we haven't looked at it in a while.
2:22
Okay, so
now let's create our first page here.
2:26
Pages of index zero will
equal a new Page object and
2:29
now we can set the properties
pages[0].setImageId().
2:34
And we need story images, now you should
already have them included in the project
2:41
from earlier when we imported
the images for the main activity.
2:45
But if you need them,
they are available for
2:48
download as a zip file in
the teacher's notes below.
2:50
We can just reference the images
using our R class just like IDs and
2:52
strings are available in code
by typing R.id or string,
2:57
we can do R.drawable to access
the drawable resources in our project.
3:01
If we hit dot, we should be able
to select all of our options and
3:07
since this is page 0,
go ahead and select that.
3:10
All right, next we need the text.
3:13
Pages at index 0,
set the textId to R dot and
3:15
here we have string instead of drawable
and we also have the name page0.
3:20
Next we need to set our two choices.
3:26
So we do pages at index 0 and we can
setChoice 1 with a new Choice object.
3:29
And we'll do the same thing for
setChoice2, another new Choice object.
3:35
So we need to add details for
the choices here.
3:43
But hang on, I actually want
to change this whole thing.
3:45
This is a great example of where a custom
constructor can come in really handy.
3:47
Let's take a short break and
then we'll clean this up.
3:51
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