Home > Mobile >  Small white consult with content providers to realize data sharing across applications
Small white consult with content providers to realize data sharing across applications

Time:02-08

Through the content provider interface can be added to a database table data, but can't update, and delete, don't know where I went wrong, please instruct the following code package com. Example. Databasetest; Import android. Content. ContentProvider; The import android. Content. ContentValues; Import android. Content. UriMatcher; The import android. Database. Cursor; The import android. Database. Sqlite. SQLiteDatabase; The import android.net.Uri; Public class DatabaseProvider extends ContentProvider {public static final ints BOOK_DIR=0; Public static final ints BOOK_ITEM=1; Public static final ints CATEGORY_DIR=2; Public static final ints CATEGORY_ITEM=3; Public static final String AUTHORITY="com. Example. Databasetest. The provider". Private static UriMatcher UriMatcher; Private MyDatabaseHelper dbHelper; The static {uriMatcher=new uriMatcher (uriMatcher NO_MATCH); UriMatcher. AddURI (AUTHORITY, "book", BOOK_DIR); UriMatcher. AddURI (AUTHORITY, "book/#", BOOK_ITEM); UriMatcher. AddURI (AUTHORITY, "category", CATEGORY_DIR); UriMatcher. AddURI (AUTHORITY, "the category/#", CATEGORY_ITEM); } public DatabaseProvider () {} @ Override public int the delete (Uri Uri, String selection, String [] selectionArgs) {//Implement this to handle requests to delete one or more rows. The SQLiteDatabase db.=dbHelper getWritableDatabase (); Int deletedRows=0; The switch (uriMatcher. Match (uri) {case BOOK_DIR: deletedRows=db. Delete (" Book ", selection, selectionArgs); break; Case BOOK_ITEM: String bookId=uri. GetPathSegments () get (1); DeletedRows=db. Delete (" Book ", "id=?" , new String [] {bookId}); break; Case CATEGORY_DIR: deletedRows=db. Delete (" Category ", selection, selectionArgs); break; Case CATEGORY_ITEM: String categoryId=uri. GetPathSegments () get (1); DeletedRows=db. Delete (" Category ", "id=?" , new String [] {categoryId}); break; Default: break; } return deletedRows;//throw new UnsupportedOperationException (" Not yet implemented "); } @ Override public String getType (Uri Uri) {//TODO: Implement this to handle requests for the MIME type of the data//at the given Uri. The switch (uriMatcher. Match (Uri) {case BOOK_DIR: return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book"; Case BOOK_ITEM: return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.book"; Case CATEGORY_DIR: return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.category"; Case CATEGORY_ITEM: return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.category"; } return null;//throw new UnsupportedOperationException (" Not yet implemented "); } @ Override public Uri inserts (Uri Uri, ContentValues values) {//TODO: Implement this to handle requests to insert a new row. The SQLiteDatabase db.=dbHelper getWritableDatabase (); Uri uriReturn=null; The switch (uriMatcher. Match (uri) {case BOOK_DIR: case BOOK_ITEM: long newBookId=db. Insert (" Book ", null values); UriReturn=Uri. Parse (" content://"+ +"/book "AUTHORITY + newBookId); break; Case CATEGORY_DIR: case CATEGORY_ITEM: long newCategoryId=the insert (" Category ", null values); UriReturn=Uri. Parse (" the content//"+ +"/category "+ newCategoryId AUTHORITY); break; Default: break; } return uriReturn;//throw new UnsupportedOperationException (" Not yet implemented "); } @ Override public Boolean onCreate () {//TODO: Implement this to initialize your content provider on startup. The dbHelper=new MyDatabaseHelper (getContext (), "BookStore. Db", null, 2); return true; } @ Override public Cursor query (Uri Uri, String [] the projection, String selection, String [] selectionArgs, String sortOrder) {//TODO: Implement this to handle query requests from clients. The SQLiteDatabase db.=dbHelper getReadableDatabase (); Cursor Cursor=null; Switch (uriMatcher. Match (uri) {case BOOK_DIR: cursor=db query (" Book ", the projection, selection, selectionArgs, null, null, sortOrder); break; Case BOOK_ITEM: String bookId=uri. GetPathSegments () get (1); Cursor=db. The query (" Book ", the projection, "id=?" , new String [] {bookId}, null, null, sortOrder); break; Case CATEGORY_DIR: cursor=db. Query (" Category ", the projection, selection, selectionArgs, null, null, sortOrder); break; Case CATEGORY_ITEM: String categoryId=uri. GetPathSegments () get (1); Cursor=db. The query (" Category ", the projection, "id=?" , new String [] {categoryId}, null, null, sortOrder); break; Default: break; } return cursor;//throw new UnsupportedOperationException (" Not yet implemented "); } @ Override public int the update (Uri Uri, ContentValues values, String selection, String [] selectionArgs) {//TODO: Implement this to handle requests to update one or more rows. The SQLiteDatabase db.=dbHelper getWritableDatabase (); Int updatedRows=0; The switch (uriMatcher. Match (uri) {case BOOK_DIR: updatedRows=db. The update (" Book ", values, selection, selectionArgs); break; null
  • Related