Customized content provider

濮俭
2023-12-01
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sqlite"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:name="com.example.sqlite.BookStoreProvider"
            android:authorities="com.example.sqlite.provider"
           <span style="font-size:24px;"> <span style="color:#ff0000;">android:exported="true"</span></span> > <!--这个真不能省-->
        </provider>
    </application>

</manifest>


package com.example.sqlite;

import com.example.sqlite.DBHelper.TABLE_Book;
import com.example.sqlite.DBHelper.TABLE_Category;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class BookStoreProvider extends ContentProvider {

	public static final String AUTORITY = "com.example.sqlite.provider";

	public static final int BOOK_DIR = 0;

	public static final int BOOK_ITEM = 1;;

	public static final int CATEGORY_DIR = 2;

	public static final int CATEGORY_ITEM = 3;

	private static UriMatcher matcher;

	private DBHelper helper;

	static {
		matcher = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI(AUTORITY, "book", BOOK_DIR);
		matcher.addURI(AUTORITY, "book/#", BOOK_ITEM);
		matcher.addURI(AUTORITY, "category", CATEGORY_DIR);
		matcher.addURI(AUTORITY, "category/#", CATEGORY_ITEM);
	}

	@Override
	public boolean onCreate() {

		helper = new DBHelper(getContext(), null, 2);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = null;
		switch (matcher.match(uri)) {
		case BOOK_DIR:
			cursor = db.query(DBHelper.TABLE_Book.TABLE_NAME, projection, selection, selectionArgs, null, null,
					sortOrder);
			break;

		case BOOK_ITEM:
			String bookID = uri.getPathSegments().get(1);
			cursor = db.query(DBHelper.TABLE_Book.TABLE_NAME, projection, DBHelper.TABLE_Book.COL_ID + " = ?",
					new String[] { bookID }, null, null, null);

			break;

		case CATEGORY_DIR:
			cursor = db.query(DBHelper.TABLE_Category.TABLE_NAME, projection, selection, selectionArgs, null, null,
					sortOrder);
			break;

		case CATEGORY_ITEM:
			String cateID = uri.getPathSegments().get(1);
			cursor = db.query(DBHelper.TABLE_Category.TABLE_NAME, projection, DBHelper.TABLE_Category.COL_CateID
					+ " = ?", new String[] { cateID }, null, null, null);
			break;
		default:
			break;
		}

		return cursor;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		switch (matcher.match(uri)) {
		case BOOK_DIR:
			return "vnd.android.cursor.dir/vnd.com.example.sqlite.provider.book";

		case BOOK_ITEM:
			return "vnd.android.cursor.item/vnd.com.example.sqlite.provider.book";

		case CATEGORY_DIR:
			return "vnd.android.cursor.dir/vnd.com.example.sqlite.provider.category";

		case CATEGORY_ITEM:
			return "vnd.android.cursor.item/vnd.com.example.sqlite.provider.category";

		}
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub

		SQLiteDatabase db = helper.getWritableDatabase();
		Uri returnUri = null;
		long newID = -1;

		switch (matcher.match(uri)) {
		case BOOK_DIR:
		case BOOK_ITEM:

			newID = db.insert(DBHelper.TABLE_Book.TABLE_NAME, null, values);
			returnUri = Uri.parse("content://" + AUTORITY + "/Book/" + newID);
			break;

		case CATEGORY_DIR:
		case CATEGORY_ITEM:
			newID = db.insert(DBHelper.TABLE_Category.TABLE_NAME, null, values);
			returnUri = Uri.parse("content://" + AUTORITY + "/Category/" + newID);
			break;
		default:
			break;
		}

		return returnUri;
	}

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

		SQLiteDatabase db = helper.getWritableDatabase();
		int id = 0;

		switch (matcher.match(uri)) {
		case BOOK_DIR:
		case BOOK_ITEM:
			id = db.delete(TABLE_Book.TABLE_NAME, selection, selectionArgs);
			break;

		case CATEGORY_DIR:
		case CATEGORY_ITEM:
			id = db.delete(TABLE_Category.TABLE_NAME, selection, selectionArgs);
			break;
		default:
			break;
		}
		return id;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = helper.getWritableDatabase();
		int rowsAffected = 0;

		switch (matcher.match(uri)) {
		case BOOK_DIR:
		case BOOK_ITEM:
			rowsAffected = db.update(TABLE_Book.TABLE_NAME, values, selection, selectionArgs);

			break;
		case CATEGORY_DIR:
		case CATEGORY_ITEM:
			rowsAffected = db.update(TABLE_Category.TABLE_NAME, values, selection, selectionArgs);
			break;
		default:
			break;
		}
		return rowsAffected;
	}

}


 类似资料:

相关阅读

相关文章

相关问答