学习ContentProvider

1.新建一个内容提供者,在mainfest文件中注册
<provider android:name="com.example.person.PersonDBProvider" 
android:authorities="com.example.person.personprovider"
android:exported="true" > </provider>


package com.example.person;

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 PersonDBProvider extends ContentProvider {
	private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
	private static final int INSERT = 1;
	private static final int DELETE = 2;
	private static final int UPDATE = 3;
	private static final int QUERY = 4;
	private static final int QUERYONE = 5;
	private PersonSQLiteOpenHelper helper;

	//添加匹配器
	static {
		matcher.addURI("com.example.person.personprovider", "insert", INSERT);
		matcher.addURI("com.example.person.personprovider", "update", UPDATE);
		matcher.addURI("com.example.person.personprovider", "delete", DELETE);
		matcher.addURI("com.example.person.personprovider", "query", QUERY);
		matcher.addURI("com.example.person.personprovider", "query/#", QUERYONE);
	}

	//内容提供者创建时调用
	@Override
	public boolean onCreate() {
		helper = new PersonSQLiteOpenHelper(getContext());
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		if (matcher.match(uri) == QUERY) {
			SQLiteDatabase db = helper.getReadableDatabase();
			Cursor cursor = db.query("person", projection, selection,
					selectionArgs, null, null, sortOrder);
			return cursor;
		} else if (matcher.match(uri) == QUERYONE) {
			// 返回所对应的id
			long id = ContentUris.parseId(uri);
			SQLiteDatabase db = helper.getReadableDatabase();
			Cursor cursor = db.query("person", projection, "id=?",
					new String[] { id + "" }, null, null, sortOrder);
			return cursor;
		} else {
			throw new IllegalArgumentException("路径不合法");
		}
	}

	@Override
	public String getType(Uri uri) {
		if (matcher.match(uri) == QUERY) {
			//返回多条数据
			return "vnd.android.cursor.dir/person";
		} else if (matcher.match(uri) == QUERYONE) {
			//返回一条数据
			return "vnd.android.cursor.item/person";
		}
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		if (matcher.match(uri) == INSERT) {
			SQLiteDatabase db = helper.getWritableDatabase();
			db.insert("person", null, values);
		} else {
			throw new IllegalArgumentException("路径不合法");
		}
		return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		if (matcher.match(uri) == DELETE) {
			SQLiteDatabase db = helper.getWritableDatabase();
			db.delete("person", selection, selectionArgs);
		} else {
			throw new IllegalArgumentException("路径不合法");
		}
		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		if (matcher.match(uri) == UPDATE) {
			SQLiteDatabase db = helper.getWritableDatabase();
			db.update("person", values, selection, selectionArgs);
		} else {
			throw new IllegalArgumentException("路径不合法");
		}
		return 0;
	}

}


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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.person.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.person.PersonDBProvider"
            android:authorities="com.example.person.personprovider"
            android:exported="true" >
        </provider>
    </application>

</manifest>



 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值