1 00:00:00,470 --> 00:00:02,560 We just finished looking over the app, and 2 00:00:02,560 --> 00:00:06,430 now we're tasked with adding in a middleman between us and our data. 3 00:00:06,430 --> 00:00:09,540 This middleman of course is the content provider. 4 00:00:09,540 --> 00:00:11,647 So let's start by creating a new class. 5 00:00:14,985 --> 00:00:17,195 And let's it call MyContentProvider. 6 00:00:20,545 --> 00:00:26,469 Then let's make it extend ContentProvider And 7 00:00:26,469 --> 00:00:29,733 use Alt+Enter to implement the six required methods. 8 00:00:33,681 --> 00:00:37,331 Then let's use Alt+Enter a bunch more to get rid of these warnings. 9 00:00:47,719 --> 00:00:48,840 All right. 10 00:00:48,840 --> 00:00:50,370 Now that we've got our content provider, 11 00:00:50,370 --> 00:00:53,780 we need to turn it into a middleman between us and our database. 12 00:00:54,810 --> 00:00:57,541 Let's start by declaring a field for the context. 13 00:01:00,605 --> 00:01:04,781 Private Context, and we'll call it context. 14 00:01:08,603 --> 00:01:10,960 And then let's create another field for our database. 15 00:01:12,570 --> 00:01:18,600 private SQLiteDatabase, and we'll call it database. 16 00:01:20,200 --> 00:01:27,139 Then, inside the onCreate function, let's add a line at the top, 17 00:01:27,139 --> 00:01:32,040 and set our context field equal to getContext. 18 00:01:32,040 --> 00:01:38,460 Then, let's create a new database helper object, DatabaseHelper, 19 00:01:38,460 --> 00:01:43,813 and we'll call it databaseHelper = new DatabaseHelper. 20 00:01:43,813 --> 00:01:45,140 And we need to parse in our context. 21 00:01:46,890 --> 00:01:51,493 Next, let's populate our database field by typing 22 00:01:51,493 --> 00:01:56,419 database = databaseHelper.getWritableDatabase. 23 00:01:58,800 --> 00:02:04,130 And finally, instead of returning false, we should really be returning whether or 24 00:02:04,130 --> 00:02:07,170 not the provider was started successfully. 25 00:02:07,170 --> 00:02:11,584 So let's return database not equal null, 26 00:02:11,584 --> 00:02:18,337 to return true if the database exists, and false if it doesn't. 27 00:02:18,337 --> 00:02:19,086 Awesome. 28 00:02:19,086 --> 00:02:21,775 That's one function down, and five to go. 29 00:02:21,775 --> 00:02:27,070 And actually, our app doesn't have the ability to delete or update data. 30 00:02:27,070 --> 00:02:28,270 So it's more like three to go. 31 00:02:29,380 --> 00:02:33,435 But before we move on to the query function, let's quickly talk about URIs. 32 00:02:34,550 --> 00:02:37,815 URI, which stands for Uniform Resource Identifier, 33 00:02:37,815 --> 00:02:42,250 is pretty much just the address of a resource in Android. 34 00:02:42,250 --> 00:02:45,670 Just like how we use URLs to access web pages, 35 00:02:45,670 --> 00:02:50,650 we view URIs to access Android resources, like content providers. 36 00:02:50,650 --> 00:02:55,450 So each content provider has its own URI that we can use to access it. 37 00:02:57,000 --> 00:02:58,850 Getting back to the query function, 38 00:02:58,850 --> 00:03:02,240 this function takes in the parameters of a SQL query, and 39 00:03:02,240 --> 00:03:06,926 should return a cursor containing the results of that query, though currently, 40 00:03:06,926 --> 00:03:12,490 we've had no idea what any of these parameters mean, and it returns null. 41 00:03:12,490 --> 00:03:15,570 So let's start by renaming these parameters to make more sense. 42 00:03:16,790 --> 00:03:18,755 Let's name the second one columns, 43 00:03:22,290 --> 00:03:26,256 The third one selection, 44 00:03:26,256 --> 00:03:30,763 the next one selectionArgs, 45 00:03:30,763 --> 00:03:35,300 and then finally sortOrder. 46 00:03:35,300 --> 00:03:39,580 And now, hopefully it's a little easier to see what's going on here. 47 00:03:39,580 --> 00:03:42,680 It's basically just a standard SQL statement. 48 00:03:42,680 --> 00:03:46,150 We select the columns from the URI, and 49 00:03:46,150 --> 00:03:51,200 then the where clause is just the selection combined with the selectionArgs, 50 00:03:51,200 --> 00:03:55,340 by replacing any question marks in the selection with the next argument. 51 00:03:56,880 --> 00:04:01,218 Lastly, we can set the order by clause by using the sortOrder parameter. 52 00:04:02,430 --> 00:04:04,190 Moving on to the inside of the function, 53 00:04:05,230 --> 00:04:09,450 we just need to return a cursor containing the results of this query. 54 00:04:09,450 --> 00:04:13,380 Let's start by creating a new SQL Lite Query Builder Object, named builder. 55 00:04:15,230 --> 00:04:19,175 SQLLiteQueryBuilder, and we'll call it builder, and 56 00:04:19,175 --> 00:04:22,060 set it equal to new SQLiteQueryBuilder. 57 00:04:23,750 --> 00:04:26,710 Then we need to set which table we're going to query 58 00:04:26,710 --> 00:04:30,802 by calling the set table's method on our builder and parsing in our VICS table. 59 00:04:31,840 --> 00:04:39,140 So builder.setTables and parsing in our VICS table, DatabaseHelper.TABLE VICS. 60 00:04:41,420 --> 00:04:42,040 And finally, 61 00:04:42,040 --> 00:04:45,788 we just need to return the result of calling the query method on our builder. 62 00:04:45,788 --> 00:04:53,060 So return builder.query, which takes in a few parameters. 63 00:04:53,060 --> 00:04:55,830 For the first parameter, let's parse in our database, 64 00:04:55,830 --> 00:05:01,100 which is just database, and then the second parameter is projection N, 65 00:05:01,100 --> 00:05:05,090 which is just another way of referring to the columns we're selecting. 66 00:05:05,090 --> 00:05:08,244 So let's just parse in columns, and 67 00:05:08,244 --> 00:05:13,680 then let's parse in selection and selectionArgs. 68 00:05:13,680 --> 00:05:18,800 And now, if we like, we can add a group by or having clause too. 69 00:05:18,800 --> 00:05:23,719 But we won't be needing either of those, so let's just parse in null and 70 00:05:23,719 --> 00:05:27,930 null, and then let's finish up by parsing in our sortOrder. 71 00:05:29,500 --> 00:05:32,260 Nice, next step is the getType function. 72 00:05:33,710 --> 00:05:38,680 This function returns the media type of the content in the specified URI. 73 00:05:38,680 --> 00:05:42,320 This way, other applications can call our getType method 74 00:05:42,320 --> 00:05:45,670 to see what kind of content we are providing. 75 00:05:45,670 --> 00:05:48,070 So if all our data happened to be HTML code, 76 00:05:48,070 --> 00:05:53,170 we might return something like text/html, or 77 00:05:53,170 --> 00:05:58,270 if it was all PNGs, maybe image/png. 78 00:05:58,270 --> 00:06:01,790 And if we want, we can even make up our own media types. 79 00:06:01,790 --> 00:06:03,670 If you'd like to read more about media types, 80 00:06:03,670 --> 00:06:04,980 check out the teacher's notes below. 81 00:06:06,110 --> 00:06:10,336 Also, if you don't need to tell other applications what kind of content you're 82 00:06:10,336 --> 00:06:12,720 providing, then you don't really need to return anything. 83 00:06:13,780 --> 00:06:19,262 So I'll just change this back to return null, And 84 00:06:19,262 --> 00:06:22,000 then the next video, we'll start in on the insert function.