<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>阳春面的学习摘录 &#187; ContentProvider</title>
	<atom:link href="http://chenyc.info/tag/contentprovider/feed/" rel="self" type="application/rss+xml" />
	<link>http://chenyc.info</link>
	<description>人生是一个不断折腾的过程，生命不息，折腾不止</description>
	<lastBuildDate>Mon, 06 Feb 2012 12:48:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>编写自己的ContentProvider</title>
		<link>http://chenyc.info/2010/03/create-contentprovider/</link>
		<comments>http://chenyc.info/2010/03/create-contentprovider/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 14:26:14 +0000</pubDate>
		<dc:creator>阳春面</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[ContentProvider]]></category>

		<guid isPermaLink="false">http://chenyc.info/?p=108</guid>
		<description><![CDATA[刚开始看Android NotePad中的ContentProvider的实现时，看的云里雾里，但自己模仿写过一个后，才发现也就这么一回事，就是实现公用的增删改查。下面将需要实现的方法做一个简单的回顾： 1.一个ContentProvider可以实现对多个数据表的操作，但每一个数据表都需要有一个独立URI，也必须有一个独立的类型。URI是其他应用访问这个数据入口，比如： content://com.chenyc.timeaccount.provider/eventtypes/id 它包括4部分，content://就是固定的头部，com.chenyc.timeaccount.provider部分需要一个唯一的字符串，一般就用ContentProiver类所在的包名，eventtypes部分一般是指在这个ContentProvider下，你需要操作那种类型的数据，一般可以用表名来表示，id部分是指具体操作数据的_id，如果查询某一条数据，则id部分就是其在数据库中的_id字段的值。 每个数据表需要有一个独立的数据类型，需要在getType(Uri uri)中实现，返回一个唯一的字符串即可，比如：vnd.chenyc.cursor.dir/vnd.account.eventtype 2.query方法，查询作为最常用的方法，实现也很简单,projection参数代表要查那些列，selection是where条件部分，selectionArgs是where条件部分参数的值,sortOrder指排序，switch部分判断是查一条数据，还是查一个list,然后根据情况进行查询 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); switch (sUriMatcher.match(uri)) { case EVENT_TYPES: qb.setTables(EVENT_TYPE_TABLE_NAME); qb.setProjectionMap(sEventTypesProjectionMap); break; case EVENT_TYPE_ID: qb.setTables(EVENT_TYPE_TABLE_NAME); qb.setProjectionMap(sEventTypesProjectionMap); qb.appendWhere(EventTypeAdapter.KEY_ROWID + &#8230; <a href="http://chenyc.info/2010/03/create-contentprovider/">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>刚开始看Android NotePad中的ContentProvider的实现时，看的云里雾里，但自己模仿写过一个后，才发现也就这么一回事，就是实现公用的增删改查。下面将需要实现的方法做一个简单的回顾：</p>
<p>1.一个ContentProvider可以实现对多个数据表的操作，但每一个数据表都需要有一个独立URI，也必须有一个独立的类型。URI是其他应用访问这个数据入口，比如：</p>
<p><a title="content://" href="content://">content://</a>com.chenyc.timeaccount.provider/eventtypes/id</p>
<p>它包括4部分，<a title="content://" href="content://">content://</a>就是固定的头部，com.chenyc.timeaccount.provider部分需要一个唯一的字符串，一般就用ContentProiver类所在的包名，eventtypes部分一般是指在这个ContentProvider下，你需要操作那种类型的数据，一般可以用表名来表示，id部分是指具体操作数据的_id，如果查询某一条数据，则id部分就是其在数据库中的_id字段的值。</p>
<p>每个数据表需要有一个独立的数据类型，需要在getType(Uri uri)中实现，返回一个唯一的字符串即可，比如：vnd.chenyc.cursor.dir/vnd.account.eventtype</p>
<p>2.query方法，查询作为最常用的方法，实现也很简单,projection参数代表要查那些列，selection是where条件部分，selectionArgs是where条件部分参数的值,sortOrder指排序，switch部分判断是查一条数据，还是查一个list,然后根据情况进行查询</p>
<pre>
public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		switch (sUriMatcher.match(uri)) {
		case EVENT_TYPES:
			qb.setTables(EVENT_TYPE_TABLE_NAME);
			qb.setProjectionMap(sEventTypesProjectionMap);
			break;
		case EVENT_TYPE_ID:
			qb.setTables(EVENT_TYPE_TABLE_NAME);
			qb.setProjectionMap(sEventTypesProjectionMap);
			qb.appendWhere(EventTypeAdapter.KEY_ROWID + "="
					+ uri.getPathSegments().get(1));
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor c = qb.query(db, projection, selection, selectionArgs, null,
				null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}
</pre>
<p>3.delete方法，处理方式也跟查询差不多，也分删一个和删一批</p>
<pre>
public int delete(Uri uri, String where, String[] whereArgs) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count;
		switch (sUriMatcher.match(uri)) {
		case EVENT_TYPES:
			count = db.delete(EVENT_TYPE_TABLE_NAME, where, whereArgs);
			break;
		case EVENT_TYPE_ID:
			String id = uri.getPathSegments().get(1);
			count = db.delete(EVENT_TYPE_TABLE_NAME,
					EventTypeAdapter.KEY_ROWID
							+ "="
							+ id
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}
</pre>
<p>4.insert方法，插入成功后需要返回这条记录的URI</p>
<pre>
public Uri insert(Uri uri, ContentValues initialValues) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		long rowId = 0;
		Uri contentUri;
		switch (sUriMatcher.match(uri)) {
		case EVENT_TYPES:
			rowId = db.insert(EVENT_TYPE_TABLE_NAME, "null", initialValues);
			contentUri = TimeAccount.EVENT_TYPE_CONTENT_URI;
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		if (rowId > 0) {
			Uri returnUri = ContentUris.withAppendedId(contentUri, rowId);
			getContext().getContentResolver().notifyChange(returnUri, null);
			return returnUri;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}
</pre>
<p>5.update方法，更新成功后需要返回修改的记录数</p>
<pre>
public int update(Uri uri, ContentValues values, String where,
			String[] whereArgs) {

		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		String id;
		int count;
		switch (sUriMatcher.match(uri)) {
		case EVENT_TYPES:
			count = db.update(EVENT_TYPE_TABLE_NAME, values, where, whereArgs);
			break;
		case EVENT_TYPE_ID:
			id = uri.getPathSegments().get(1);
			count = db.update(EVENT_TYPE_TABLE_NAME, values,
					EventTypeAdapter.KEY_ROWID
							+ "="
							+ id
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://chenyc.info/2010/03/create-contentprovider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

