1 00:00:00,720 --> 00:00:03,560 All right, so we can pass simple data back and forth. 2 00:00:03,560 --> 00:00:07,410 We've actually seen how to pass complex data around in an earlier course too. 3 00:00:07,410 --> 00:00:09,810 Do you remember the parcelable interface? 4 00:00:09,810 --> 00:00:12,660 It's a specific type of serialization that allows us to pass 5 00:00:12,660 --> 00:00:15,330 more complex data around in a standard way. 6 00:00:15,330 --> 00:00:18,560 Let's look at an example here with complex data and a recycler view. 7 00:00:20,130 --> 00:00:25,030 In the project, we have eight models package, and inside is a song class. 8 00:00:25,030 --> 00:00:28,010 It contains a bit of information about a specific song like, 9 00:00:28,010 --> 00:00:30,760 the artist ,the year released and more. 10 00:00:30,760 --> 00:00:35,230 The goal here is to transfer an instance of a song from one activity to another. 11 00:00:35,230 --> 00:00:37,120 Now we could break this down piece by piece and 12 00:00:37,120 --> 00:00:41,450 attach each of these pieces of data as separate extras to one intent, but 13 00:00:41,450 --> 00:00:43,850 hopefully you see that that could quickly get cumbersome and 14 00:00:43,850 --> 00:00:47,350 also potentially impossible as our models get more and more complex. 15 00:00:48,460 --> 00:00:51,640 Next, let's take a look at the recycler view used in main activity. 16 00:00:51,640 --> 00:00:52,870 Let's start by looking at the layout. 17 00:00:52,870 --> 00:00:57,880 If we expand rez, then layout, and open activity_main, 18 00:00:57,880 --> 00:01:00,580 here we can see a list of items above our buttons, and 19 00:01:00,580 --> 00:01:04,200 here in the text view, we can see we're using the recyclerView element. 20 00:01:05,310 --> 00:01:09,780 The Recycler View uses list item song for individual items, and in here 21 00:01:09,780 --> 00:01:14,050 we have an image view, which will show a heart icon when a song is favorited. 22 00:01:14,050 --> 00:01:16,640 It's hidden by default, which is why we don't see it over here, and 23 00:01:16,640 --> 00:01:18,110 a text view which will show the title. 24 00:01:19,660 --> 00:01:23,070 By the way, I got this heart icon from the Google material design. 25 00:01:23,070 --> 00:01:25,740 Icon pack which is a really useful set of artwork. 26 00:01:25,740 --> 00:01:28,520 You can even download individual icons from right here 27 00:01:28,520 --> 00:01:29,665 by clicking on the one that you want. 28 00:01:29,665 --> 00:01:33,490 You can change the density, you can change the color and 29 00:01:33,490 --> 00:01:34,720 download in different formats. 30 00:01:37,280 --> 00:01:39,790 I put the link in teachers notes and I'd recommend bookmarking it for 31 00:01:39,790 --> 00:01:40,550 future reference. 32 00:01:41,910 --> 00:01:44,630 Now let's open up a recycler view adapter. 33 00:01:44,630 --> 00:01:47,860 We have an adapters package and inside is a playlist adapter. 34 00:01:49,260 --> 00:01:52,130 Recycler views get a little tricky when it comes to TAPS and 35 00:01:52,130 --> 00:01:55,210 intents and results, because if you remember, the best practice for 36 00:01:55,210 --> 00:02:00,400 recycler views is to define a view holder that binds the data to each item. 37 00:02:00,400 --> 00:02:02,580 We have a view holder down here called SongViewHolder. 38 00:02:03,790 --> 00:02:07,920 This means that taps are detected in code is triggered from each view holder, 39 00:02:07,920 --> 00:02:11,760 which we see down here we have bind song which binds it to the view and 40 00:02:11,760 --> 00:02:12,610 an onClick method. 41 00:02:14,135 --> 00:02:17,880 Let's add the code to send a whole song to the detail view to see what I mean. 42 00:02:19,070 --> 00:02:22,840 I'm going to add a few lines to bump up my code and then here in the onClick method. 43 00:02:22,840 --> 00:02:25,110 Let's start by declaring an explicit intent. 44 00:02:26,230 --> 00:02:31,880 Intent equals new intent and we'll pass in the context. 45 00:02:31,880 --> 00:02:35,300 Now normally in an activity the context is readily available. 46 00:02:35,300 --> 00:02:37,150 Here we need to make sure that it's passed in. 47 00:02:37,150 --> 00:02:39,920 We're passing it in when we create the playlists adapter and 48 00:02:39,920 --> 00:02:41,960 we're storing it as mContext. 49 00:02:41,960 --> 00:02:42,550 So, let's use that. 50 00:02:43,770 --> 00:02:49,790 We want to go to the Detailactivity-class, and next we want to put 51 00:02:49,790 --> 00:02:55,140 the whole song as an extra intent that put extra, check up this list it 52 00:02:55,140 --> 00:02:59,860 includes a put extra overload that takes a parcelable value as one of its parameters. 53 00:03:00,990 --> 00:03:06,250 So let's use for the first parameter the existing key mainActivity.EXTRA_SONG. 54 00:03:06,250 --> 00:03:08,790 Actually, the original 55 00:03:08,790 --> 00:03:12,200 key from the original version of this project is slightly different key. 56 00:03:12,200 --> 00:03:14,890 So let's fix that real quick so that we're consistent. 57 00:03:14,890 --> 00:03:18,812 So go to main_activity, and up at the top select KEY_SONG. 58 00:03:18,812 --> 00:03:24,740 Right-click and select Refactor > Rename we want our keys to be consistent. 59 00:03:25,740 --> 00:03:28,130 While it doesn't really matter what we name our keys, 60 00:03:28,130 --> 00:03:31,480 it helps to have a consistent format, so let's call this extra song, and 61 00:03:31,480 --> 00:03:37,195 let's also change the value so that it's consistent will repeat extra _SONG. 62 00:03:37,195 --> 00:03:42,335 Okay, so back in playlist adapter, we can now access this extra song, and 63 00:03:42,335 --> 00:03:44,985 now we can parcel in the song at the current position. 64 00:03:44,985 --> 00:03:46,995 We can access this from mSongs, 65 00:03:46,995 --> 00:03:50,345 which is an array with a method called getAdapterPosition. 66 00:03:52,400 --> 00:03:55,850 One tricky thing about recycler views is letting the parent activity know which 67 00:03:55,850 --> 00:03:57,350 item is being acted on. 68 00:03:57,350 --> 00:04:00,225 We're going to need that position, so let's just add it as another extra. 69 00:04:00,225 --> 00:04:07,389 Intent.putExtra and we need another key, let's define it here. 70 00:04:07,389 --> 00:04:13,211 Mainactivity.EXTRA_LIST_POSITION. 71 00:04:14,360 --> 00:04:17,070 Now if we hover over this, and let's try quick fix. 72 00:04:17,070 --> 00:04:21,660 If we hit alt plus enter, we can create a new constant field extra list position. 73 00:04:21,660 --> 00:04:23,420 Let's see what happens. 74 00:04:23,420 --> 00:04:25,850 Look at that, it's got pretty much everything we need. 75 00:04:25,850 --> 00:04:31,081 Let's just come over here and type in our value EXTRA_LIST_POSITION, very nice. 76 00:04:32,220 --> 00:04:33,650 Once again back in playlist adapter. 77 00:04:33,650 --> 00:04:37,940 Now we can pass in the position by calling getAdapterPosition once again. 78 00:04:39,560 --> 00:04:41,900 Finally we need to start the detail activity. 79 00:04:41,900 --> 00:04:44,500 So let's call startActivity and 80 00:04:44,500 --> 00:04:47,650 wait a minute, why is an auto complete showing anything here? 81 00:04:47,650 --> 00:04:51,230 Remember the startActivity methods are in the activity class but 82 00:04:51,230 --> 00:04:55,150 this is an adapter class, we need to reference the parent activity. 83 00:04:55,150 --> 00:04:57,170 So, do we know what that is from here? 84 00:04:57,170 --> 00:04:59,240 We do. We are passing in the context when 85 00:04:59,240 --> 00:05:03,810 we create this adapter, and we're storing it in mContext, we could change that to 86 00:05:03,810 --> 00:05:07,120 pass in an activity explicitly, but let's just do it with a cast here. 87 00:05:07,120 --> 00:05:09,860 So add some parentheses, notice there's double parentheses. 88 00:05:09,860 --> 00:05:15,980 And we're going to cast to Activity, it's mContext, and with the second 89 00:05:15,980 --> 00:05:19,720 set of run the C's, we can now use the .syntax to call start activity for result. 90 00:05:20,750 --> 00:05:24,315 We'll pass in our intent, and we'll use a request code the same as before 91 00:05:24,315 --> 00:05:29,419 MainActivity.RequestFavorite.