Androidカスタムコンテンツプロバイダ使用
8192 ワード
android , , , 。 contentProvider, uri, contentResolver ,getContentResolver() contentProvider 。
/** * Description: <br/> * Copyright (c) , 2016, Jansonxu <br/> * This program is protected by copyright laws. <br/> * Program Name:UserContentProvider.java <br/> * Date: 2016 3 9 * * @author * @version : 1.0 */
package com.example.contentprovider.provider;
import com.example.contentprovider.util.Dbhelper;
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;
public class UserContentProvider extends ContentProvider {
//① ContentProvider + ( 。 )
//
public static final String AUTHORITY = "com.example.contentprovider.user_1";
//② CODE
public static final int CODE_USER = 1;
public static final int CODE_ORDER = 2;
//③ UriMatcher
public static UriMatcher uriMatcher; // uri
//
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);//
// t_user Uri content://com.example.contentprovider.user_1/user
uriMatcher.addURI(AUTHORITY, "user", CODE_USER); //path
uriMatcher.addURI(AUTHORITY, "order", CODE_ORDER); //path
}
/* * */
private Dbhelper dbHelper;
@Override
public boolean onCreate() {
//
dbHelper = new Dbhelper(getContext());
return false;
}
/* (non-Javadoc) * @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String) */
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
int code = uriMatcher.match(uri);// uri code
Cursor cursor = null;
SQLiteDatabase db = dbHelper.getReadableDatabase();
switch (code) {
case CODE_USER:
// t_user User
cursor = db.query("t_user", projection, selection, selectionArgs, null, null, sortOrder);
break;
case CODE_ORDER:
break;
default:
break;
}
return cursor;
}
/* (non-Javadoc) * @see android.content.ContentProvider#getType(android.net.Uri) */
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
/* (non-Javadoc) * @see android.content.ContentProvider#insert(android.net.Uri, android.content.ContentValues) */
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
if(uriMatcher.match(uri)==UserContentProvider.CODE_USER){
long id = db.insert("t_user", null, values);
return ContentUris.withAppendedId(uri, id);
}
return null;
}
/* (non-Javadoc) * @see android.content.ContentProvider#delete(android.net.Uri, java.lang.String, java.lang.String[]) */
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int num = 0;
if(uriMatcher.match(uri)==UserContentProvider.CODE_USER){
num = db.delete("t_user", selection, selectionArgs);
}
return num;
}
/* (non-Javadoc) * @see android.content.ContentProvider#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[]) */
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
if(uriMatcher.match(uri)==UserContentProvider.CODE_USER){
db.update("t_user", values, selection, selectionArgs);
}
return 0;
}
}