MyProvider.java

 

package com.luofei.cp;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class MyProvider extends ContentProvider {
 MyDataBase myDB;
 SQLiteDatabase mySqliteDB;
 
 private static final UriMatcher sMatcher; 
    static{
     //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)
        sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //如果match()方法匹配content://com.changcheng.sqlite.provider.contactprovider/contact路径,返回匹配码为1
        sMatcher.addURI(Utils.AUTOHORITY,Utils.TNAME, Utils.ITEM);
        //如果match()方法匹配   content://com.changcheng.sqlite.provider.contactprovider/contact/230路径,返回匹配码为2
        sMatcher.addURI(Utils.AUTOHORITY, Utils.TNAME+"/#", Utils.ITEM_ID);
    }
 
 @Override
 public int delete(Uri uri, String selection, String[] selectionArgs) {
  // TODO Auto-generated method stub
  mySqliteDB = myDB.getWritableDatabase();
        int count = 0;
        switch (sMatcher.match(uri)) {
        case Utils.ITEM:
                count = mySqliteDB.delete(Utils.TNAME,selection, selectionArgs);
                break;
        case Utils.ITEM_ID:
                String id = uri.getPathSegments().get(1);
                count = mySqliteDB.delete(Utils.TID, Utils.TID+"="+id+(!TextUtils.isEmpty(Utils.TID="?")?"AND("+selection+')':""), selectionArgs);
            break;
        default:
                throw new IllegalArgumentException("Unknown URI"+uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
 }

 @Override
 public String getType(Uri uri) {
  // TODO Auto-generated method stub
  switch (sMatcher.match(uri)) {
        case Utils.ITEM:
                return Utils.CONTENT_TYPE;
        case Utils.ITEM_ID:
            return Utils.CONTENT_ITEM_TYPE;
        default:
                throw new IllegalArgumentException("Unknown URI"+uri);
        }
  
 }

 @Override
 public Uri insert(Uri uri, ContentValues values) {
  // TODO Auto-generated method stub
  mySqliteDB = myDB.getWritableDatabase();
        long rowId;
        if(sMatcher.match(uri)!=Utils.ITEM){
                throw new IllegalArgumentException("Unknown URI"+uri);
        }
        rowId = mySqliteDB.insert(Utils.TNAME,Utils.TID,values);
           if(rowId>0){
                   Uri noteUri=ContentUris.withAppendedId(Utils.CONTENT_URI, rowId);
                   getContext().getContentResolver().notifyChange(noteUri, null);
                   return noteUri;
           }
           throw new IllegalArgumentException("Unknown URI"+uri);
 }

 @Override
 public boolean onCreate() {
  // TODO Auto-generated method stub
  this.myDB=new MyDataBase(this.getContext());
  return true;
 }

 @Override
 public Cursor query(Uri uri, String[] projection, String selection,
   String[] selectionArgs, String sortOrder) {
  // TODO Auto-generated method stub
  this.mySqliteDB=myDB.getWritableDatabase();
  Cursor c;
        Log.d("-------", String.valueOf(sMatcher.match(uri)));
        switch (sMatcher.match(uri)) {
        case Utils.ITEM:
                c = this.mySqliteDB.query(Utils.TNAME, projection, selection, selectionArgs, null, null, null);
        
                break;
        case Utils.ITEM_ID:
                String id = uri.getPathSegments().get(1);
                c = mySqliteDB.query(Utils.TNAME, projection, Utils.TID+"="+id+(!TextUtils.isEmpty(selection)?"AND("+selection+')':""),selectionArgs, null, null, sortOrder);
            break;
        default:
                Log.d("!!!!!!", "Unknown URI"+uri);
                throw new IllegalArgumentException("Unknown URI"+uri);
        }
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
  
 }

 @Override
 public int update(Uri uri, ContentValues values, String selection,
   String[] selectionArgs) {
  // TODO Auto-generated method stub
  return 0;
 }

}

你可能感兴趣的:(string,null,insert,delete,query,class)