contentprovider


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.util.Log;

public class Studentprovider extends ContentProvider {

    public static final int VERSION = 3;

    public static final String AUTOHORITY = "com.my.student";

    public static final int ITEM = 1;

    public static final int ITEM_ID = 2;

    public static final String CONTENT_TYPE = "vnd.android.cursor.item/vnd.my.test";

    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTOHORITY + "/"
            + StudentDBHelper.TABLE_NAME);

    private static final UriMatcher sMatcher;

    static {
        sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sMatcher.addURI(AUTOHORITY, StudentDBHelper.TABLE_NAME, ITEM);
        sMatcher.addURI(AUTOHORITY, StudentDBHelper.TABLE_NAME + "/#", ITEM_ID);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub

        SQLiteDatabase db = StudentDBHelper.getSQLiteDatabse(this.getContext());
        int count = 0;

        switch (sMatcher.match(uri)) {
            case ITEM:
                count = db.delete(StudentDBHelper.TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        }

        getContext().getContentResolver().notifyChange(uri, null);
        db.close();

        return 0;
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        
        return null;
    }

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

        db.close();
        throw new IllegalArgumentException("Unknown URI" + uri);

    }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        // TODO Auto-generated method stub

        SQLiteDatabase db = StudentDBHelper.getSQLiteDatabse(this.getContext());
        Cursor c;

        switch (sMatcher.match(uri)) {
            case ITEM:
                c = db.query(StudentDBHelper.TABLE_NAME, projection, selection, selectionArgs,
                        null, null, null);
                break;

            default:
                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,delete,query,insert,import)