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
Passing simple data is done with various putExtra() methods, but passing complex model data requires that our model objects implement the Parcelable interface? It's a specific type of serialization that allows us to pass more complex data around in a standard way. Let's look at an example here with complex data and a RecyclerView.
Helpful Links
- Google Material Design Icons
- Android Lists and Adapters - We have a stage in this course called "Using Parcelable Data". Check it out or review it if you want to brush up on how to implement the Parcelable interface.
- Benefit of using Parcelable instead of serializing object
- parcelabler - A tool for generating some Parcelable code for existing objects.
Documentation
Downloads
All right, so
we can pass simple data back and forth.
0:00
We've actually seen how to pass complex
data around in an earlier course too.
0:03
Do you remember the parcelable interface?
0:07
It's a specific type of
serialization that allows us to pass
0:09
more complex data around
in a standard way.
0:12
Let's look at an example here with
complex data and a recycler view.
0:15
In the project, we have eight models
package, and inside is a song class.
0:20
It contains a bit of information
about a specific song like,
0:25
the artist ,the year released and more.
0:28
The goal here is to transfer an instance
of a song from one activity to another.
0:30
Now we could break this
down piece by piece and
0:35
attach each of these pieces of data
as separate extras to one intent, but
0:37
hopefully you see that that
could quickly get cumbersome and
0:41
also potentially impossible as our
models get more and more complex.
0:43
Next, let's take a look at the recycler
view used in main activity.
0:48
Let's start by looking at the layout.
0:51
If we expand rez, then layout,
and open activity_main,
0:52
here we can see a list of
items above our buttons, and
0:57
here in the text view, we can see
we're using the recyclerView element.
1:00
The Recycler View uses list item song for
individual items, and in here
1:05
we have an image view, which will show
a heart icon when a song is favorited.
1:09
It's hidden by default, which is
why we don't see it over here, and
1:14
a text view which will show the title.
1:16
By the way, I got this heart icon
from the Google material design.
1:19
Icon pack which is a really
useful set of artwork.
1:23
You can even download individual
icons from right here
1:25
by clicking on the one that you want.
1:28
You can change the density,
you can change the color and
1:29
download in different formats.
1:33
I put the link in teachers notes and
I'd recommend bookmarking it for
1:37
future reference.
1:39
Now let's open up a recycler view adapter.
1:41
We have an adapters package and
inside is a playlist adapter.
1:44
Recycler views get a little
tricky when it comes to TAPS and
1:49
intents and results, because if you
remember, the best practice for
1:52
recycler views is to define a view
holder that binds the data to each item.
1:55
We have a view holder down
here called SongViewHolder.
2:00
This means that taps are detected in
code is triggered from each view holder,
2:03
which we see down here we have bind
song which binds it to the view and
2:07
an onClick method.
2:11
Let's add the code to send a whole song
to the detail view to see what I mean.
2:14
I'm going to add a few lines to bump up my
code and then here in the onClick method.
2:19
Let's start by declaring
an explicit intent.
2:22
Intent equals new intent and
we'll pass in the context.
2:26
Now normally in an activity
the context is readily available.
2:31
Here we need to make
sure that it's passed in.
2:35
We're passing it in when we
create the playlists adapter and
2:37
we're storing it as mContext.
2:39
So, let's use that.
2:41
We want to go to the Detailactivity-class,
and next we want to put
2:43
the whole song as an extra intent
that put extra, check up this list it
2:49
includes a put extra overload that takes a
parcelable value as one of its parameters.
2:55
So let's use for the first parameter
the existing key mainActivity.EXTRA_SONG.
3:00
Actually, the original
3:06
key from the original version of this
project is slightly different key.
3:08
So let's fix that real quick so
that we're consistent.
3:12
So go to main_activity, and
up at the top select KEY_SONG.
3:14
Right-click and select Refactor > Rename
we want our keys to be consistent.
3:18
While it doesn't really
matter what we name our keys,
3:25
it helps to have a consistent format,
so let's call this extra song, and
3:28
let's also change the value so that
it's consistent will repeat extra _SONG.
3:31
Okay, so back in playlist adapter,
we can now access this extra song, and
3:37
now we can parcel in the song
at the current position.
3:42
We can access this from mSongs,
3:44
which is an array with a method
called getAdapterPosition.
3:46
One tricky thing about recycler views is
letting the parent activity know which
3:52
item is being acted on.
3:55
We're going to need that position, so
let's just add it as another extra.
3:57
Intent.putExtra and we need another key,
let's define it here.
4:00
Mainactivity.EXTRA_LIST_POSITION.
4:07
Now if we hover over this,
and let's try quick fix.
4:14
If we hit alt plus enter, we can create
a new constant field extra list position.
4:17
Let's see what happens.
4:21
Look at that,
it's got pretty much everything we need.
4:23
Let's just come over here and type in our
value EXTRA_LIST_POSITION, very nice.
4:25
Once again back in playlist adapter.
4:32
Now we can pass in the position by
calling getAdapterPosition once again.
4:33
Finally we need to start
the detail activity.
4:39
So let's call startActivity and
4:41
wait a minute, why is an auto
complete showing anything here?
4:44
Remember the startActivity methods
are in the activity class but
4:47
this is an adapter class,
we need to reference the parent activity.
4:51
So, do we know what that is from here?
4:55
We do.
We are passing in the context when
4:57
we create this adapter, and we're storing
it in mContext, we could change that to
4:59
pass in an activity explicitly, but
let's just do it with a cast here.
5:03
So add some parentheses,
notice there's double parentheses.
5:07
And we're going to cast to Activity,
it's mContext, and with the second
5:09
set of run the C's, we can now use the
.syntax to call start activity for result.
5:15
We'll pass in our intent, and we'll
use a request code the same as before
5:20
MainActivity.RequestFavorite.
5:24
You need to sign up for Treehouse in order to download course files.
Sign up