Androidアプリケーション開発のContentProvider



  
  
  
  
  1. package cn.class3g.db;  
  2.    
  3. import cn.class3g.service.DatabaseHelper;  
  4. import android.content.ContentProvider;  
  5. import android.content.ContentUris;  
  6. import android.content.ContentValues;  
  7. import android.content.UriMatcher;  
  8. import android.database.Cursor;  
  9. import android.database.sqlite.SQLiteDatabase;  
  10. import android.net.Uri;  
  11.    
  12. public class PersonProvider extends ContentProvider {  
  13.        private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  
  14.    
  15.        private static final int PERSONS = 1;  
  16.        private static final int PERSON = 2;  
  17.    
  18.        private DatabaseHelper dbHelper;  
  19.    
  20.        static {  
  21.               matcher.addURI("cn.class3g.providers.personprovider""person", PERSONS);  
  22.               matcher.addURI("cn.class3g.providers.personprovider""person/#",  
  23.                             PERSON);  
  24.        }  
  25.    
  26.        public boolean onCreate() {  
  27.               dbHelper = new DatabaseHelper(this.getContext());  
  28.               return true;  
  29.        }  
  30.    
  31.        //  content://cn.itcast.provides.personprovider/person  
  32.        public Uri insert(Uri uri, ContentValues values) {  
  33.               SQLiteDatabase db = dbHelper.getWritableDatabase();  
  34.               long rowId;  
  35.    
  36.               switch (matcher.match(uri)) {  
  37.               case PERSONS: //  
  38.                      rowId = db.insert("person""personid", values);  
  39.                      return ContentUris.withAppendedId(uri, rowId);  
  40.               default:  
  41.                      throw new IllegalArgumentException("Unknow Uri:" + uri);  
  42.               }  
  43.        }  
  44.    
  45.        public Cursor query(Uri uri, String[] projection, String selection,  
  46.                      String[] selectionArgs, String sortOrder) {  
  47.               SQLiteDatabase db = dbHelper.getReadableDatabase();  
  48.               switch (matcher.match(uri)) {  
  49.               case PERSONS:  
  50.                      return db.query("person", projection, selection, selectionArgs, nullnull, sortOrder);  
  51.               case PERSON:  
  52.                      long personid = ContentUris.parseId(uri);  
  53.                      String where = "personid="+ personid;  
  54.                      if(selection!=null && !"".equals(selection)){  
  55.                             where = where + " and "+ selection;  
  56.                      }  
  57.                      return db.query("person", projection, where, selectionArgs, nullnull, sortOrder);  
  58.                       
  59.               default:  
  60.                      throw new IllegalArgumentException("Unknown Uri:"+ uri);  
  61.               }  
  62.        }  
  63.    
  64.        //  content://cn.itcast.provides.personprovider/person   
  65.        //  content://cn.itcast.provides.personprovider/person/10  id  
  66.        public int update(Uri uri, ContentValues values, String selection,  
  67.                      String[] selectionArgs) {  
  68.               SQLiteDatabase db = dbHelper.getWritableDatabase();  
  69.               int num;  
  70.               switch(matcher.match(uri)){  
  71.               case PERSONS: //  
  72.                      num = db.update("person", values, selection, selectionArgs);  
  73.                      break;  
  74.               case PERSON:  
  75.                      long personid = ContentUris.parseId(uri);  
  76.                      String where = "personid=" + personid;  
  77.                      if(selection != null){  
  78.                             where += " and " + selection;  
  79.                      }  
  80.                      num = db.update("person", values, where, selectionArgs);  
  81.                      break;  
  82.               default:  
  83.                      throw new IllegalArgumentException("Unknow Uri"+uri);  
  84.               }  
  85.               return num;  
  86.        }  
  87.    
  88.        public int delete(Uri uri, String selection, String[] selectionArgs) {  
  89.               SQLiteDatabase db = dbHelper.getWritableDatabase();  
  90.               int num = 0;  
  91.               switch (matcher.match(uri)) {  
  92.               case PERSONS:  
  93.                      num = db.delete("person", selection, selectionArgs);  
  94.                      break;  
  95.               case PERSON:  
  96.                      long personid = ContentUris.parseId(uri);  
  97.                      String where = "personid="+ personid;  
  98.                      if(selection!=null && !"".equals(selection)){  
  99.                             where = where + " and "+ selection;  
  100.                      }  
  101.                      num = db.delete("person", where, selectionArgs);  
  102.                      break;      
  103.               default:  
  104.                      throw new IllegalArgumentException("Unknown Uri:"+ uri);  
  105.               }  
  106.               return num;  
  107.        }  
  108.    
  109.        public String getType(Uri uri) {  
  110.               switch (matcher.match(uri)) {  
  111.               case PERSONS:  
  112.                      return "vnd.android.cursor.dir/person";  
  113.               case PERSON:  
  114.                      return "vnd.android.cursor.item/person";  
  115.               default:  
  116.                      throw new IllegalArgumentException("Unknown Uri:"+ uri);  
  117.               }  
  118.        }