SlideShare ist ein Scribd-Unternehmen logo
1 von 3
package com.example.mycontentprovider; 
import java.util.HashMap; 
import android.content.ContentProvider; 
import android.content.ContentUris; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.UriMatcher; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteQueryBuilder; 
import android.net.Uri; 
import android.text.TextUtils; 
public class StudentsProvider extends ContentProvider { 
//composition de uri de provider 
static final String PROVIDER_NAME = "com.example.provider"; 
static final String URL = "content://" + PROVIDER_NAME + "/students"; 
static final public Uri CONTENT_URI = Uri.parse(URL); 
static final String _ID = "_id"; 
static final String NAME = "name"; 
static final String GRADE = "grade"; 
private static HashMap<String, String> STUDENTS_PROJECTION_MAP; 
static final int STUDENTS = 1; 
static final int STUDENT_ID = 2; 
//Creates the root node of the URI tree 
static final UriMatcher uriMatcher; 
static{ 
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
uriMatcher.addURI(PROVIDER_NAME, "students", STUDENTS); 
uriMatcher.addURI(PROVIDER_NAME, "students/#", STUDENT_ID); 
} 
/** 
* Database specific constant declarations 
*/ 
DatabaseHelper dbHelper ; 
private SQLiteDatabase db; 
// création et ouverture de la base college.db 
@Override 
public boolean onCreate() { 
Context context = getContext(); 
//création de la base college.db 
dbHelper = new DatabaseHelper(context); 
/** 
*open data base to practice the CRUD operations 
*/ 
db = dbHelper.getWritableDatabase(); 
return (db == null)? false:true; 
} 
@Override 
public Uri insert(Uri uri, ContentValues values) {
/** 
* Add a new student record 
*/ 
long rowID = db.insert(dbHelper.STUDENTS_TABLE_NAME, "", values); 
/** 
* If record is added successfully 
*/ 
if (rowID > 0) 
{ 
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID); 
getContext().getContentResolver().notifyChange(_uri, null); 
return _uri; 
} 
throw new SQLException("Failed to add a record into " + uri); 
} 
@Override 
public Cursor query(Uri uri, String[] projection, String selection, 
String[] selectionArgs, String sortOrder) { 
SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 
qb.setTables(dbHelper.STUDENTS_TABLE_NAME); 
switch (uriMatcher.match(uri)) { 
case STUDENTS: 
qb.setProjectionMap(STUDENTS_PROJECTION_MAP); 
break; 
case STUDENT_ID: 
qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1)); 
break; 
default: 
throw new IllegalArgumentException("Unknown URI " + uri); 
} 
if (sortOrder == null || sortOrder == ""){ 
/** 
* By default sort on student names 
*/ 
sortOrder = NAME; 
} 
Cursor c = qb.query(db, projection, selection, selectionArgs, 
null, null, sortOrder); 
/** 
* register to watch a content URI for changes 
*/ 
c.setNotificationUri(getContext().getContentResolver(), uri); 
return c; 
} 
@Override 
public int delete(Uri uri, String selection, String[] selectionArgs) { 
int count = 0; 
switch (uriMatcher.match(uri)){ 
case STUDENTS: 
count = db.delete(dbHelper.STUDENTS_TABLE_NAME, selection, 
selectionArgs); 
break; 
case STUDENT_ID: 
String id = uri.getPathSegments().get(1); 
count = db.delete( dbHelper.STUDENTS_TABLE_NAME, _ID + " = " + id + 
(!TextUtils.isEmpty(selection) ? " AND (" + 
selection + ')' : ""), selectionArgs); 
break;
default: 
throw new IllegalArgumentException("Unknown URI " + uri); 
} 
getContext().getContentResolver().notifyChange(uri, null); 
return count; 
} 
@Override 
public int update(Uri uri, ContentValues values, String selection, 
String[] selectionArgs) { 
int count = 0; 
switch (uriMatcher.match(uri)){ 
case STUDENTS: 
count = db.update(dbHelper.STUDENTS_TABLE_NAME, values, 
selection, selectionArgs); 
break; 
case STUDENT_ID: 
count = db.update(dbHelper.STUDENTS_TABLE_NAME, values, _ID + 
" = " + uri.getPathSegments().get(1) + 
(!TextUtils.isEmpty(selection) ? " AND (" + 
selection + ')' : ""), selectionArgs); 
break; 
default: 
throw new IllegalArgumentException("Unknown URI " + uri ); 
} 
getContext().getContentResolver().notifyChange(uri, null); 
return count; 
} 
@Override 
public String getType(Uri uri) { 
switch (uriMatcher.match(uri)){ 
/** 
* Get all student records 
*/ 
case STUDENTS: 
return "vnd.android.cursor.dir/vnd.example.students"; 
/** 
* Get a particular student 
*/ 
case STUDENT_ID: 
return "vnd.android.cursor.item/vnd.example.students"; 
default: 
throw new IllegalArgumentException("Unsupported URI: " + uri); 
} 
} 
}

Weitere ähnliche Inhalte

Was ist angesagt?

JavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJSJavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
philogb
 
Func dyn statement_set.c
Func dyn statement_set.cFunc dyn statement_set.c
Func dyn statement_set.c
albertinous
 
Working With JQuery Part1
Working With JQuery Part1Working With JQuery Part1
Working With JQuery Part1
saydin_soft
 

Was ist angesagt? (20)

Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - Guilin
 
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJSJavaScript para Graficos y Visualizacion de Datos - BogotaJS
JavaScript para Graficos y Visualizacion de Datos - BogotaJS
 
Angular 2.0 Views
Angular 2.0 ViewsAngular 2.0 Views
Angular 2.0 Views
 
Senten500.c
Senten500.cSenten500.c
Senten500.c
 
Viastudy ef core_cheat_sheet
Viastudy ef core_cheat_sheetViastudy ef core_cheat_sheet
Viastudy ef core_cheat_sheet
 
Formacion en movilidad: Conceptos de desarrollo en iOS (IV)
Formacion en movilidad: Conceptos de desarrollo en iOS (IV) Formacion en movilidad: Conceptos de desarrollo en iOS (IV)
Formacion en movilidad: Conceptos de desarrollo en iOS (IV)
 
When cqrs meets event sourcing
When cqrs meets event sourcingWhen cqrs meets event sourcing
When cqrs meets event sourcing
 
안드로이드 데이터 바인딩
안드로이드 데이터 바인딩안드로이드 데이터 바인딩
안드로이드 데이터 바인딩
 
662305 09
662305 09662305 09
662305 09
 
Simplifying JavaScript Projects with ReactJS
Simplifying JavaScript Projects with ReactJSSimplifying JavaScript Projects with ReactJS
Simplifying JavaScript Projects with ReactJS
 
MongoDB Aggregation
MongoDB Aggregation MongoDB Aggregation
MongoDB Aggregation
 
06 jQuery #burningkeyboards
06 jQuery  #burningkeyboards06 jQuery  #burningkeyboards
06 jQuery #burningkeyboards
 
Func dyn statement_set.c
Func dyn statement_set.cFunc dyn statement_set.c
Func dyn statement_set.c
 
jQuery Datatables With MongDb
jQuery Datatables With MongDbjQuery Datatables With MongDb
jQuery Datatables With MongDb
 
Working With JQuery Part1
Working With JQuery Part1Working With JQuery Part1
Working With JQuery Part1
 
Using Arbor/ RGraph JS libaries for Data Visualisation
Using Arbor/ RGraph JS libaries for Data VisualisationUsing Arbor/ RGraph JS libaries for Data Visualisation
Using Arbor/ RGraph JS libaries for Data Visualisation
 
JavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerJavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primer
 
Hack tutorial
Hack tutorialHack tutorial
Hack tutorial
 
JavaScript Performance 20160723
JavaScript Performance 20160723JavaScript Performance 20160723
JavaScript Performance 20160723
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Ähnlich wie F3

Testdrevet javautvikling på objektorienterte skinner
Testdrevet javautvikling på objektorienterte skinnerTestdrevet javautvikling på objektorienterte skinner
Testdrevet javautvikling på objektorienterte skinner
Truls Jørgensen
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
Darwin Durand
 
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
ヘルスケアサービスを実現する最新技術  〜HealthKit・GCP + Goの活用〜ヘルスケアサービスを実現する最新技術  〜HealthKit・GCP + Goの活用〜
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
DeNA
 
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
ヘルスケアサービスを実現する最新技術  〜HealthKit・GCP + Goの活用〜ヘルスケアサービスを実現する最新技術  〜HealthKit・GCP + Goの活用〜
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
DeNA
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
Alexey Buzdin
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
C.T.Co
 
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docxDAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
theodorelove43763
 

Ähnlich wie F3 (20)

CDI e as ideias pro futuro do VRaptor
CDI e as ideias pro futuro do VRaptorCDI e as ideias pro futuro do VRaptor
CDI e as ideias pro futuro do VRaptor
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java script
 
Testdrevet javautvikling på objektorienterte skinner
Testdrevet javautvikling på objektorienterte skinnerTestdrevet javautvikling på objektorienterte skinner
Testdrevet javautvikling på objektorienterte skinner
 
The Principle of Hybrid App.
The Principle of Hybrid App.The Principle of Hybrid App.
The Principle of Hybrid App.
 
F1
F1F1
F1
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
ヘルスケアサービスを実現する最新技術  〜HealthKit・GCP + Goの活用〜ヘルスケアサービスを実現する最新技術  〜HealthKit・GCP + Goの活用〜
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
 
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
ヘルスケアサービスを実現する最新技術  〜HealthKit・GCP + Goの活用〜ヘルスケアサービスを実現する最新技術  〜HealthKit・GCP + Goの活用〜
ヘルスケアサービスを実現する最新技術 〜HealthKit・GCP + Goの活用〜
 
ヘルスケアサービスを実現する最新技術
〜HealthKit・GCP+Go〜
ヘルスケアサービスを実現する最新技術
〜HealthKit・GCP+Go〜ヘルスケアサービスを実現する最新技術
〜HealthKit・GCP+Go〜
ヘルスケアサービスを実現する最新技術
〜HealthKit・GCP+Go〜
 
F2
F2F2
F2
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Android dev toolbox
Android dev toolboxAndroid dev toolbox
Android dev toolbox
 
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docxDAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
 
Jsr 303
Jsr 303Jsr 303
Jsr 303
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 
Introduction to Java Programming 10th ed --25-4 (Implement preorder t.docx
Introduction to Java Programming 10th ed  --25-4 (Implement preorder t.docxIntroduction to Java Programming 10th ed  --25-4 (Implement preorder t.docx
Introduction to Java Programming 10th ed --25-4 (Implement preorder t.docx
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Practical Model View Programming (Roadshow Version)
Practical Model View Programming (Roadshow Version)Practical Model View Programming (Roadshow Version)
Practical Model View Programming (Roadshow Version)
 
MVM - It's all in the (Implementation) Details
MVM - It's all in the (Implementation) DetailsMVM - It's all in the (Implementation) Details
MVM - It's all in the (Implementation) Details
 

Mehr von Saber LAJILI (16)

Cours android user_interface_2016
Cours android user_interface_2016Cours android user_interface_2016
Cours android user_interface_2016
 
Cours android 2016
Cours android 2016Cours android 2016
Cours android 2016
 
Les ateliers android_1_vers2015
Les ateliers android_1_vers2015Les ateliers android_1_vers2015
Les ateliers android_1_vers2015
 
4 asynch task_services_thread
4 asynch task_services_thread4 asynch task_services_thread
4 asynch task_services_thread
 
Accueil.java
Accueil.javaAccueil.java
Accueil.java
 
Activity accueil
Activity accueilActivity accueil
Activity accueil
 
Main.xml
Main.xmlMain.xml
Main.xml
 
Liste exposés université privée leaders
Liste exposés université privée leadersListe exposés université privée leaders
Liste exposés université privée leaders
 
1 tours horizon
1 tours horizon1 tours horizon
1 tours horizon
 
5 android web_service
5 android web_service5 android web_service
5 android web_service
 
Exemple de création de base
Exemple de création de baseExemple de création de base
Exemple de création de base
 
Mini projet android 2014 2015 iset nabeul sem3
Mini projet android 2014 2015 iset nabeul sem3Mini projet android 2014 2015 iset nabeul sem3
Mini projet android 2014 2015 iset nabeul sem3
 
Les capteurs sous android
Les capteurs sous androidLes capteurs sous android
Les capteurs sous android
 
3 shared preference_sq_lite
3 shared preference_sq_lite3 shared preference_sq_lite
3 shared preference_sq_lite
 
4 asynch task_services_thread
4 asynch task_services_thread4 asynch task_services_thread
4 asynch task_services_thread
 
2 activity user_interface
2 activity user_interface2 activity user_interface
2 activity user_interface
 

Kürzlich hochgeladen

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Kürzlich hochgeladen (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

F3

  • 1. package com.example.mycontentprovider; import java.util.HashMap; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.UriMatcher; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.text.TextUtils; public class StudentsProvider extends ContentProvider { //composition de uri de provider static final String PROVIDER_NAME = "com.example.provider"; static final String URL = "content://" + PROVIDER_NAME + "/students"; static final public Uri CONTENT_URI = Uri.parse(URL); static final String _ID = "_id"; static final String NAME = "name"; static final String GRADE = "grade"; private static HashMap<String, String> STUDENTS_PROJECTION_MAP; static final int STUDENTS = 1; static final int STUDENT_ID = 2; //Creates the root node of the URI tree static final UriMatcher uriMatcher; static{ uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(PROVIDER_NAME, "students", STUDENTS); uriMatcher.addURI(PROVIDER_NAME, "students/#", STUDENT_ID); } /** * Database specific constant declarations */ DatabaseHelper dbHelper ; private SQLiteDatabase db; // création et ouverture de la base college.db @Override public boolean onCreate() { Context context = getContext(); //création de la base college.db dbHelper = new DatabaseHelper(context); /** *open data base to practice the CRUD operations */ db = dbHelper.getWritableDatabase(); return (db == null)? false:true; } @Override public Uri insert(Uri uri, ContentValues values) {
  • 2. /** * Add a new student record */ long rowID = db.insert(dbHelper.STUDENTS_TABLE_NAME, "", values); /** * If record is added successfully */ if (rowID > 0) { Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID); getContext().getContentResolver().notifyChange(_uri, null); return _uri; } throw new SQLException("Failed to add a record into " + uri); } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); qb.setTables(dbHelper.STUDENTS_TABLE_NAME); switch (uriMatcher.match(uri)) { case STUDENTS: qb.setProjectionMap(STUDENTS_PROJECTION_MAP); break; case STUDENT_ID: qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1)); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } if (sortOrder == null || sortOrder == ""){ /** * By default sort on student names */ sortOrder = NAME; } Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder); /** * register to watch a content URI for changes */ c.setNotificationUri(getContext().getContentResolver(), uri); return c; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int count = 0; switch (uriMatcher.match(uri)){ case STUDENTS: count = db.delete(dbHelper.STUDENTS_TABLE_NAME, selection, selectionArgs); break; case STUDENT_ID: String id = uri.getPathSegments().get(1); count = db.delete( dbHelper.STUDENTS_TABLE_NAME, _ID + " = " + id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); break;
  • 3. default: throw new IllegalArgumentException("Unknown URI " + uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int count = 0; switch (uriMatcher.match(uri)){ case STUDENTS: count = db.update(dbHelper.STUDENTS_TABLE_NAME, values, selection, selectionArgs); break; case STUDENT_ID: count = db.update(dbHelper.STUDENTS_TABLE_NAME, values, _ID + " = " + uri.getPathSegments().get(1) + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); break; default: throw new IllegalArgumentException("Unknown URI " + uri ); } getContext().getContentResolver().notifyChange(uri, null); return count; } @Override public String getType(Uri uri) { switch (uriMatcher.match(uri)){ /** * Get all student records */ case STUDENTS: return "vnd.android.cursor.dir/vnd.example.students"; /** * Get a particular student */ case STUDENT_ID: return "vnd.android.cursor.item/vnd.example.students"; default: throw new IllegalArgumentException("Unsupported URI: " + uri); } } }