SlideShare a Scribd company logo
Ahmad Arif Faizin
Google Associate Android Developer, Curriculum Developer Dicoding
Penyedia data untuk aplikasi lain
// Sets the columns to retrieve for the user profile
val projection = arrayOf(
ContactsContract.Profile._ID,
ContactsContract.Profile.DISPLAY_NAME_PRIMARY,
ContactsContract.Profile.PHOTO_THUMBNAIL_URI,
)
// Retrieves the profile from the Contacts Provider
val profileCursor = contentResolver.query(
ContactsContract.Profile.CONTENT_URI,
projection,
null,null,null //selection, selectionArgs, sortOrder
)
● ContentResolver : used to access data in a content provider. The
ContentResolver methods provide the basic "CRUD" (create, read, update, and
delete) functions of persistent storage.
● Content URIs : used to identify data in a provider. Content URIs include the
symbolic name of the entire provider (its authority) and a name that points to a
table (a path).
● Provider : a class that inherits to ContentProvider to implement CRUD operation
to database.
1. Buat Provider Class yang extends ke ContentProvider
2. Daftarkan Provider Class di Manifest
3. Tentukan Content URI
4. Tambah URI ke Contract Class
5. Buat URI Matcher
6. Lengkapi fungsi CRUD yang dibutuhkan
7. Coba untuk akses menggunakan ContentResolver
class NoteProvider : ContentProvider() {
private lateinit var noteHelper: NoteHelper
override fun onCreate(): Boolean {
// database initialization
noteHelper = NoteHelper.getInstance(context as Context)
noteHelper.open()
return true
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dicoding.picodiploma.mynotesapp">
<permission android:name="com.dicoding.picodiploma.mynotesapp.READ_DATABASE"
android:protectionLevel="normal" />
<permission android:name="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"
android:protectionLevel="normal" />
<application...>
...
<provider
android:authorities="com.dicoding.picodiploma.mynotesapp"
android:name=".provider.NoteProvider"
android:exported="true"
android:readPermission="com.dicoding.picodiploma.mynotesapp.READ_DATABASE"
android:writePermission="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"/>
</application>
</manifest>
● Format:
<scheme>://<authorities>/<path>
<scheme>://<authorities>/<path>/# (untuk number)
<scheme>://<authorities>/<path>/* (untuk string)
● Example:
○ Get All Note:
content://com.dicoding.picodiploma.mynotesapp/note
○ Note with id:
content://com.dicoding.picodiploma.mynotesapp/note/#
object DatabaseContract {
const val AUTHORITY = "com.dicoding.picodiploma.mynotesapp"
const val SCHEME = "content"
class NoteColumns : BaseColumns {
companion object {
…
//result = content://com.dicoding.picodiploma.mynotesapp/note
val CONTENT_URI: Uri = Uri.Builder()
.scheme(SCHEME)
.authority(AUTHORITY)
.appendPath(TABLE_NAME)
.build()
}
}
}
class NoteProvider : ContentProvider() {
companion object {
private const val NOTE = 1
private const val NOTE_ID = 2
private val sUriMatcher = UriMatcher(UriMatcher.NO_MATCH)
init {
// content://com.dicoding.picodiploma.mynotesapp/note
sUriMatcher.addURI(AUTHORITY, TABLE_NAME, NOTE)
// content://com.dicoding.picodiploma.mynotesapp/note/id
sUriMatcher.addURI(AUTHORITY, "$TABLE_NAME/#", NOTE_ID)
}
}
...
}
class NoteProvider : ContentProvider() {
override fun query(uri: Uri, strings: Array<String>?, s: String?,
strings1: Array<String>?, s1: String?): Cursor? {
return when (sUriMatcher.match(uri)) {
// content://com.dicoding.picodiploma.mynotesapp/note
NOTE -> noteHelper.queryAll()
// content://com.dicoding.picodiploma.mynotesapp/note/1
NOTE_ID -> noteHelper.queryById(uri.lastPathSegment.toString())
else -> null
}
}
override fun insert(...): Uri? {...}
override fun update(...): Int {...}
override fun delete(...): Int {...}
}
val uriWithId = Uri.parse(CONTENT_URI.toString() + "/" + note?.id)
val cursor = contentResolver.query(uriWithId, null, null, null, null)
val cursor = contentResolver.query(CONTENT_URI,null, null, null, null)
● Get All Data
● Get Data by id
contentResolver.update(uriWithId, values, null, null)
contentResolver.insert(CONTENT_URI, values)
● Insert Data
● Update Data
contentResolver.delete(uriWithId, null, null)
● Delete Data
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dicoding.picodiploma.consumerapp">
<uses-permission android:name="com.dicoding.picodiploma.mynotesapp.READ_DATABASE"/>
<uses-permission android:name="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"/>
...
<queries>
<package android:name="com.dicoding.picodiploma.mynotesapp" />
</queries>
</manifest>
Contact:
@arif_faizin
Follow us: @dicoding

More Related Content

What's hot

Ame concepts
Ame conceptsAme concepts
Ame concepts
cstath
 
Statement of Goals
Statement of GoalsStatement of Goals
Statement of Goals
Brandi Soto
 
Fusion hcm roles information
Fusion hcm roles informationFusion hcm roles information
Fusion hcm roles information
Santosh Mankala
 
Java (Netbeans) Polymorphism - Object Oriented Programming (OOP)
Java (Netbeans) Polymorphism - Object Oriented Programming (OOP)Java (Netbeans) Polymorphism - Object Oriented Programming (OOP)
Java (Netbeans) Polymorphism - Object Oriented Programming (OOP)
Melina Krisnawati
 

What's hot (18)

Php with mysql ppt
Php with mysql pptPhp with mysql ppt
Php with mysql ppt
 
Mobile Store
Mobile StoreMobile Store
Mobile Store
 
Ame concepts
Ame conceptsAme concepts
Ame concepts
 
Html hyperlinks
Html hyperlinksHtml hyperlinks
Html hyperlinks
 
Create bar chart report for p6 r8.3 using bi publisher 11g - Oracle Primavera...
Create bar chart report for p6 r8.3 using bi publisher 11g - Oracle Primavera...Create bar chart report for p6 r8.3 using bi publisher 11g - Oracle Primavera...
Create bar chart report for p6 r8.3 using bi publisher 11g - Oracle Primavera...
 
Statement of Goals
Statement of GoalsStatement of Goals
Statement of Goals
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
online shopping
online shopping online shopping
online shopping
 
Introduction to Oracle
Introduction to OracleIntroduction to Oracle
Introduction to Oracle
 
Android App Development - 05 Action bar
Android App Development - 05 Action barAndroid App Development - 05 Action bar
Android App Development - 05 Action bar
 
HPE NonStop SQL WebDBS - Introduction
HPE NonStop SQL WebDBS - IntroductionHPE NonStop SQL WebDBS - Introduction
HPE NonStop SQL WebDBS - Introduction
 
Simple xml in .net
Simple xml in .netSimple xml in .net
Simple xml in .net
 
Extensions in OAF
Extensions in OAF Extensions in OAF
Extensions in OAF
 
5 retro pay_methods_i___ii_part_5
5 retro pay_methods_i___ii_part_55 retro pay_methods_i___ii_part_5
5 retro pay_methods_i___ii_part_5
 
Fusion hcm roles information
Fusion hcm roles informationFusion hcm roles information
Fusion hcm roles information
 
Java (Netbeans) Polymorphism - Object Oriented Programming (OOP)
Java (Netbeans) Polymorphism - Object Oriented Programming (OOP)Java (Netbeans) Polymorphism - Object Oriented Programming (OOP)
Java (Netbeans) Polymorphism - Object Oriented Programming (OOP)
 
Fusion absence management explained with examples
Fusion absence management   explained with examplesFusion absence management   explained with examples
Fusion absence management explained with examples
 
6 things you need to know about GORM 6
6 things you need to know about GORM 66 things you need to know about GORM 6
6 things you need to know about GORM 6
 

Similar to Dicoding Developer Coaching #20: Android | Apa itu Content Provider?

Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012 Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Atlassian
 
Android app development basics
Android app development basicsAndroid app development basics
Android app development basics
Anton Narusberg
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
fiyuer
 

Similar to Dicoding Developer Coaching #20: Android | Apa itu Content Provider? (20)

Beginning Native Android Apps
Beginning Native Android AppsBeginning Native Android Apps
Beginning Native Android Apps
 
Daniel Kachakil - Android's Download Provider: Discovering and exploiting thr...
Daniel Kachakil - Android's Download Provider: Discovering and exploiting thr...Daniel Kachakil - Android's Download Provider: Discovering and exploiting thr...
Daniel Kachakil - Android's Download Provider: Discovering and exploiting thr...
 
Android Froyo
Android FroyoAndroid Froyo
Android Froyo
 
Android開発の基礎_20101218
Android開発の基礎_20101218Android開発の基礎_20101218
Android開発の基礎_20101218
 
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012 Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
Plugins on OnDemand with Remote Apps - Atlassian Summit 2012
 
Training Session 2 - Day 2
Training Session 2 - Day 2Training Session 2 - Day 2
Training Session 2 - Day 2
 
Android Training (Content Provider)
Android Training (Content Provider)Android Training (Content Provider)
Android Training (Content Provider)
 
Android app development basics
Android app development basicsAndroid app development basics
Android app development basics
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Native Android Development Practices
Native Android Development PracticesNative Android Development Practices
Native Android Development Practices
 
Synapseindia android apps introduction hello world
Synapseindia android apps introduction hello worldSynapseindia android apps introduction hello world
Synapseindia android apps introduction hello world
 
Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications  Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications
 
Android Tutorials - Powering with Selection Widget
Android Tutorials - Powering with Selection WidgetAndroid Tutorials - Powering with Selection Widget
Android Tutorials - Powering with Selection Widget
 
Connecting your Python App to OpenERP through OOOP
Connecting your Python App to OpenERP through OOOPConnecting your Python App to OpenERP through OOOP
Connecting your Python App to OpenERP through OOOP
 
Android Workshop
Android WorkshopAndroid Workshop
Android Workshop
 
What's new in Android O
What's new in Android OWhat's new in Android O
What's new in Android O
 
Volley lab btc_bbit
Volley lab btc_bbitVolley lab btc_bbit
Volley lab btc_bbit
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015
 
Reaching out from ADF Mobile (ODTUG KScope 2014)
Reaching out from ADF Mobile (ODTUG KScope 2014)Reaching out from ADF Mobile (ODTUG KScope 2014)
Reaching out from ADF Mobile (ODTUG KScope 2014)
 
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & MenusLearn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
Learn Xamarin Absolute Beginners - Permissions, Building the App GUI & Menus
 

More from DicodingEvent

More from DicodingEvent (20)

Developer Coaching #114.pdf
Developer Coaching #114.pdfDeveloper Coaching #114.pdf
Developer Coaching #114.pdf
 
Ask Us Anything about Studi Independen Bersertifikat Kampus Merdeka X Dicodin...
Ask Us Anything about Studi Independen Bersertifikat Kampus Merdeka X Dicodin...Ask Us Anything about Studi Independen Bersertifikat Kampus Merdeka X Dicodin...
Ask Us Anything about Studi Independen Bersertifikat Kampus Merdeka X Dicodin...
 
tantangan menjadi developer di abad 21
tantangan menjadi developer di abad 21tantangan menjadi developer di abad 21
tantangan menjadi developer di abad 21
 
Mengenalkan augmented reality (ar) pada snapchat
Mengenalkan augmented reality (ar) pada snapchatMengenalkan augmented reality (ar) pada snapchat
Mengenalkan augmented reality (ar) pada snapchat
 
Membangun Aplikasi Serverless di Platfrom AWS
Membangun Aplikasi Serverless di Platfrom AWSMembangun Aplikasi Serverless di Platfrom AWS
Membangun Aplikasi Serverless di Platfrom AWS
 
IDCamp X Madrasah: Pengenalan Computational Thinking
IDCamp X Madrasah: Pengenalan Computational ThinkingIDCamp X Madrasah: Pengenalan Computational Thinking
IDCamp X Madrasah: Pengenalan Computational Thinking
 
Membuat Produk Digital Terbaik ala Startup Unicorn
Membuat Produk Digital Terbaik ala Startup UnicornMembuat Produk Digital Terbaik ala Startup Unicorn
Membuat Produk Digital Terbaik ala Startup Unicorn
 
TechTalk 2021: Peran IT Security dalam Penerapan DevOps
TechTalk 2021: Peran IT Security dalam Penerapan DevOpsTechTalk 2021: Peran IT Security dalam Penerapan DevOps
TechTalk 2021: Peran IT Security dalam Penerapan DevOps
 
TechTalk 2021: Peningkatan Performa Software Delivery dengan CI/CD
TechTalk 2021: Peningkatan Performa Software Delivery dengan CI/CDTechTalk 2021: Peningkatan Performa Software Delivery dengan CI/CD
TechTalk 2021: Peningkatan Performa Software Delivery dengan CI/CD
 
Membuat Solusi Bermanfaat dengan Programming - Nur Rohman
Membuat Solusi Bermanfaat dengan Programming - Nur RohmanMembuat Solusi Bermanfaat dengan Programming - Nur Rohman
Membuat Solusi Bermanfaat dengan Programming - Nur Rohman
 
Potensi karier menjadi ios developer di masa depan
Potensi karier menjadi ios developer di masa depanPotensi karier menjadi ios developer di masa depan
Potensi karier menjadi ios developer di masa depan
 
Id camp x dicoding live : persiapan jadi software engineer hebat 101
Id camp x dicoding live : persiapan jadi software engineer hebat 101Id camp x dicoding live : persiapan jadi software engineer hebat 101
Id camp x dicoding live : persiapan jadi software engineer hebat 101
 
Tips sukses berkarir sebagai developer dan programmer 2021
Tips sukses berkarir sebagai developer dan programmer 2021Tips sukses berkarir sebagai developer dan programmer 2021
Tips sukses berkarir sebagai developer dan programmer 2021
 
Teknologi Baru Android di Google I/O 2021 - Andrew Kurniadi
Teknologi Baru Android di Google I/O 2021 - Andrew KurniadiTeknologi Baru Android di Google I/O 2021 - Andrew Kurniadi
Teknologi Baru Android di Google I/O 2021 - Andrew Kurniadi
 
Dicoding Developer Coaching #38: Android | 5 Library Android yang Patut Kamu ...
Dicoding Developer Coaching #38: Android | 5 Library Android yang Patut Kamu ...Dicoding Developer Coaching #38: Android | 5 Library Android yang Patut Kamu ...
Dicoding Developer Coaching #38: Android | 5 Library Android yang Patut Kamu ...
 
Dicoding Developer Coaching #37: Android | Kesalahan yang Sering Terjadi pada...
Dicoding Developer Coaching #37: Android | Kesalahan yang Sering Terjadi pada...Dicoding Developer Coaching #37: Android | Kesalahan yang Sering Terjadi pada...
Dicoding Developer Coaching #37: Android | Kesalahan yang Sering Terjadi pada...
 
Pengantar Cloud Computing dengan AWS - Petra Novandi Barus
Pengantar Cloud Computing dengan AWS - Petra Novandi BarusPengantar Cloud Computing dengan AWS - Petra Novandi Barus
Pengantar Cloud Computing dengan AWS - Petra Novandi Barus
 
Dicoding Developer Coaching #36: Android | Pentingnya Performa pada Aplikasi ...
Dicoding Developer Coaching #36: Android | Pentingnya Performa pada Aplikasi ...Dicoding Developer Coaching #36: Android | Pentingnya Performa pada Aplikasi ...
Dicoding Developer Coaching #36: Android | Pentingnya Performa pada Aplikasi ...
 
Dicoding Developer Coaching #34: Android | Modular Android App dengan Dynamic...
Dicoding Developer Coaching #34: Android | Modular Android App dengan Dynamic...Dicoding Developer Coaching #34: Android | Modular Android App dengan Dynamic...
Dicoding Developer Coaching #34: Android | Modular Android App dengan Dynamic...
 
Dicoding Developer Coaching #35: Android | Setup Continuous Integration di An...
Dicoding Developer Coaching #35: Android | Setup Continuous Integration di An...Dicoding Developer Coaching #35: Android | Setup Continuous Integration di An...
Dicoding Developer Coaching #35: Android | Setup Continuous Integration di An...
 

Recently uploaded

527598851-ppc-due-to-various-govt-policies.pdf
527598851-ppc-due-to-various-govt-policies.pdf527598851-ppc-due-to-various-govt-policies.pdf
527598851-ppc-due-to-various-govt-policies.pdf
rajpreetkaur75080
 

Recently uploaded (15)

05232024 Joint Meeting - Community Networking
05232024 Joint Meeting - Community Networking05232024 Joint Meeting - Community Networking
05232024 Joint Meeting - Community Networking
 
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdfOracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
Oracle Database Administration I (1Z0-082) Exam Dumps 2024.pdf
 
Eureka, I found it! - Special Libraries Association 2021 Presentation
Eureka, I found it! - Special Libraries Association 2021 PresentationEureka, I found it! - Special Libraries Association 2021 Presentation
Eureka, I found it! - Special Libraries Association 2021 Presentation
 
Getting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control TowerGetting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control Tower
 
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
0x01 - Newton's Third Law:  Static vs. Dynamic Abusers0x01 - Newton's Third Law:  Static vs. Dynamic Abusers
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
 
Acorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutesAcorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutes
 
Writing Sample 2 -Bridging the Divide: Enhancing Public Engagement in Urban D...
Writing Sample 2 -Bridging the Divide: Enhancing Public Engagement in Urban D...Writing Sample 2 -Bridging the Divide: Enhancing Public Engagement in Urban D...
Writing Sample 2 -Bridging the Divide: Enhancing Public Engagement in Urban D...
 
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
 
123445566544333222333444dxcvbcvcvharsh.pptx
123445566544333222333444dxcvbcvcvharsh.pptx123445566544333222333444dxcvbcvcvharsh.pptx
123445566544333222333444dxcvbcvcvharsh.pptx
 
The Canoga Gardens Development Project. PDF
The Canoga Gardens Development Project. PDFThe Canoga Gardens Development Project. PDF
The Canoga Gardens Development Project. PDF
 
Pollinator Ambassador Earth Steward Day Presentation 2024-05-22
Pollinator Ambassador Earth Steward Day Presentation 2024-05-22Pollinator Ambassador Earth Steward Day Presentation 2024-05-22
Pollinator Ambassador Earth Steward Day Presentation 2024-05-22
 
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
 
Hi-Tech Industry 2024-25 Prospective.pptx
Hi-Tech Industry 2024-25 Prospective.pptxHi-Tech Industry 2024-25 Prospective.pptx
Hi-Tech Industry 2024-25 Prospective.pptx
 
527598851-ppc-due-to-various-govt-policies.pdf
527598851-ppc-due-to-various-govt-policies.pdf527598851-ppc-due-to-various-govt-policies.pdf
527598851-ppc-due-to-various-govt-policies.pdf
 
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXOBitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXO
 

Dicoding Developer Coaching #20: Android | Apa itu Content Provider?

  • 1. Ahmad Arif Faizin Google Associate Android Developer, Curriculum Developer Dicoding
  • 2. Penyedia data untuk aplikasi lain
  • 3.
  • 4. // Sets the columns to retrieve for the user profile val projection = arrayOf( ContactsContract.Profile._ID, ContactsContract.Profile.DISPLAY_NAME_PRIMARY, ContactsContract.Profile.PHOTO_THUMBNAIL_URI, ) // Retrieves the profile from the Contacts Provider val profileCursor = contentResolver.query( ContactsContract.Profile.CONTENT_URI, projection, null,null,null //selection, selectionArgs, sortOrder )
  • 5. ● ContentResolver : used to access data in a content provider. The ContentResolver methods provide the basic "CRUD" (create, read, update, and delete) functions of persistent storage. ● Content URIs : used to identify data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). ● Provider : a class that inherits to ContentProvider to implement CRUD operation to database.
  • 6. 1. Buat Provider Class yang extends ke ContentProvider 2. Daftarkan Provider Class di Manifest 3. Tentukan Content URI 4. Tambah URI ke Contract Class 5. Buat URI Matcher 6. Lengkapi fungsi CRUD yang dibutuhkan 7. Coba untuk akses menggunakan ContentResolver
  • 7. class NoteProvider : ContentProvider() { private lateinit var noteHelper: NoteHelper override fun onCreate(): Boolean { // database initialization noteHelper = NoteHelper.getInstance(context as Context) noteHelper.open() return true } }
  • 8. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dicoding.picodiploma.mynotesapp"> <permission android:name="com.dicoding.picodiploma.mynotesapp.READ_DATABASE" android:protectionLevel="normal" /> <permission android:name="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE" android:protectionLevel="normal" /> <application...> ... <provider android:authorities="com.dicoding.picodiploma.mynotesapp" android:name=".provider.NoteProvider" android:exported="true" android:readPermission="com.dicoding.picodiploma.mynotesapp.READ_DATABASE" android:writePermission="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"/> </application> </manifest>
  • 9. ● Format: <scheme>://<authorities>/<path> <scheme>://<authorities>/<path>/# (untuk number) <scheme>://<authorities>/<path>/* (untuk string) ● Example: ○ Get All Note: content://com.dicoding.picodiploma.mynotesapp/note ○ Note with id: content://com.dicoding.picodiploma.mynotesapp/note/#
  • 10. object DatabaseContract { const val AUTHORITY = "com.dicoding.picodiploma.mynotesapp" const val SCHEME = "content" class NoteColumns : BaseColumns { companion object { … //result = content://com.dicoding.picodiploma.mynotesapp/note val CONTENT_URI: Uri = Uri.Builder() .scheme(SCHEME) .authority(AUTHORITY) .appendPath(TABLE_NAME) .build() } } }
  • 11. class NoteProvider : ContentProvider() { companion object { private const val NOTE = 1 private const val NOTE_ID = 2 private val sUriMatcher = UriMatcher(UriMatcher.NO_MATCH) init { // content://com.dicoding.picodiploma.mynotesapp/note sUriMatcher.addURI(AUTHORITY, TABLE_NAME, NOTE) // content://com.dicoding.picodiploma.mynotesapp/note/id sUriMatcher.addURI(AUTHORITY, "$TABLE_NAME/#", NOTE_ID) } } ... }
  • 12. class NoteProvider : ContentProvider() { override fun query(uri: Uri, strings: Array<String>?, s: String?, strings1: Array<String>?, s1: String?): Cursor? { return when (sUriMatcher.match(uri)) { // content://com.dicoding.picodiploma.mynotesapp/note NOTE -> noteHelper.queryAll() // content://com.dicoding.picodiploma.mynotesapp/note/1 NOTE_ID -> noteHelper.queryById(uri.lastPathSegment.toString()) else -> null } } override fun insert(...): Uri? {...} override fun update(...): Int {...} override fun delete(...): Int {...} }
  • 13. val uriWithId = Uri.parse(CONTENT_URI.toString() + "/" + note?.id) val cursor = contentResolver.query(uriWithId, null, null, null, null) val cursor = contentResolver.query(CONTENT_URI,null, null, null, null) ● Get All Data ● Get Data by id
  • 14. contentResolver.update(uriWithId, values, null, null) contentResolver.insert(CONTENT_URI, values) ● Insert Data ● Update Data contentResolver.delete(uriWithId, null, null) ● Delete Data
  • 15.
  • 16.
  • 17.
  • 18. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dicoding.picodiploma.consumerapp"> <uses-permission android:name="com.dicoding.picodiploma.mynotesapp.READ_DATABASE"/> <uses-permission android:name="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"/> ... <queries> <package android:name="com.dicoding.picodiploma.mynotesapp" /> </queries> </manifest>