标签:ati 查询 values bsp 使用 class main sch 更改
public class PersonContentProvider extends ContentProvider{ public boolean onCreate() public Uri insert(Uri uri, ContentValues values) public int delete(Uri uri, String selection, String[] selectionArgs) public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) public String getType(Uri uri) }
1 public class PersonContentProvider extends ContentProvider{ 2 private DataBaseOpenHelper dataBaseOpenHelper; 3 private static final int ALLPERSON=1; 4 private static final int PERSON=2; 5 private static final UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH); 6 static{ 7 uriMatcher.addURI("com.gao.provider.personprovider", "person", ALLPERSON); 8 uriMatcher.addURI("com.gao.provider.personprovider", "person/#", PERSON); 9 10 } 11 @Override 12 public int delete(Uri uri, String selection, String[] selectionArgs) { 13 SQLiteDatabase database=dataBaseOpenHelper.getWritableDatabase(); 14 int count=0; 15 switch (uriMatcher.match(uri)) { 16 case ALLPERSON://content://com.gao.provider.personprovider/person 17 count=database.delete("person", selection, selectionArgs); 18 break; 19 case PERSON://content://com.gao.provider.personprovider/person/100 20 long id=ContentUris.parseId(uri); 21 String where=TextUtils.isEmpty(selection)?"personid=?":selection+"and personid=?"; 22 String[] params=new String[]{String.valueOf(id)}; 23 if (!TextUtils.isEmpty(selection)&&selectionArgs!=null) { 24 params=new String[selectionArgs.length+1]; 25 for (int i = 0; i < selectionArgs.length; i++) { 26 params[i]=selectionArgs[i]; 27 } 28 params[selectionArgs.length+1]=String.valueOf(id); 29 } 30 count=database.delete("person", where, selectionArgs); 31 break; 32 default: 33 throw new IllegalArgumentException("Unkonw uri:"+uri); 34 } 35 return count; 36 } 37 @Override 38 public String getType(Uri uri) { 39 switch (uriMatcher.match(uri)) { 40 case ALLPERSON: 41 return "vnd.android.cursor.dir/personprovider.person"; 42 case PERSON: 43 return "vnd.android.cursor.item/personprovider.person"; 44 default: 45 break; 46 } 47 return null; 48 } 49 @Override 50 public Uri insert(Uri uri, ContentValues values) { 51 SQLiteDatabase database=dataBaseOpenHelper.getWritableDatabase(); 52 long id=0; 53 switch (uriMatcher.match(uri)) { 54 case ALLPERSON://content://com.gao.provider.personprovider/person 55 id=database.insert("person", "name", values);//返回记录的行号,主键是int,实际上就是主键值 56 return ContentUris.withAppendedId(uri, id); 57 case PERSON://content://com.gao.provider.personprovider/person/100 58 id=database.insert("person", "name", values); 59 String path=uri.toString(); 60 return Uri.parse(path.substring(0, path.lastIndexOf("/"))+"id"); 61 default: 62 throw new IllegalArgumentException("Unkonw uri:"+uri); 63 } 64 } 65 @Override 66 public boolean onCreate() { 67 dataBaseOpenHelper=new DataBaseOpenHelper(this.getContext()); 68 return false; 69 } 70 @Override 71 public Cursor query(Uri uri, String[] projection, String selection, 72 String[] selectionArgs, String sortOrder) { 73 SQLiteDatabase database=dataBaseOpenHelper.getReadableDatabase(); 74 switch (uriMatcher.match(uri)) { 75 case ALLPERSON: 76 database.query("person", projection, selection, selectionArgs, null, null, sortOrder); 77 break; 78 case PERSON: 79 long id=ContentUris.parseId(uri); 80 String where=TextUtils.isEmpty(selection)?"personid=?":selection+"and personid=?"; 81 String[] params=new String[]{String.valueOf(id)}; 82 if (!TextUtils.isEmpty(selection)&&selectionArgs!=null) { 83 params=new String[selectionArgs.length+1]; 84 for (int i = 0; i < selectionArgs.length; i++) { 85 params[i]=selectionArgs[i]; 86 } 87 params[selectionArgs.length+1]=String.valueOf(id); 88 } 89 database.query("person", projection, where, params, null, null, sortOrder); 90 break; 91 default: 92 break; 93 } 94 return null; 95 } 96 //update("content://com.gao.provider.personprovider/person/100",values,"name like ? and ...",new String[]{"%gao%"}); 97 @Override 98 public int update(Uri uri, ContentValues values, String selection, 99 String[] selectionArgs) { 100 SQLiteDatabase database=dataBaseOpenHelper.getWritableDatabase(); 101 int count=0; 102 switch (uriMatcher.match(uri)) { 103 case ALLPERSON://content://com.gao.provider.personprovider/person 104 count=database.update("person", values, selection,selectionArgs); 105 break; 106 case PERSON://content://com.gao.provider.personprovider/person/100 107 long id=ContentUris.parseId(uri); 108 String where=TextUtils.isEmpty(selection)?"personid=?":selection+"and personid=?"; 109 String[] params=new String[]{String.valueOf(id)}; 110 if (!TextUtils.isEmpty(selection)&&selectionArgs!=null) { 111 params=new String[selectionArgs.length+1]; 112 for (int i = 0; i < selectionArgs.length; i++) { 113 params[i]=selectionArgs[i]; 114 } 115 params[selectionArgs.length+1]=String.valueOf(id); 116 } 117 count=database.update("person", values, where,params); 118 break; 119 default: 120 throw new IllegalArgumentException("Unkonw uri:"+uri); 121 } 122 return count; 123 } 124 } 125
ContentProvider与ContentResolver使用
标签:ati 查询 values bsp 使用 class main sch 更改
原文地址:http://www.cnblogs.com/Sharley/p/6112918.html