���ļ�Ҫ������AndroidӦ�ó������Content Provider��Ӧ�ó���乲�����ݵ�ԭ��������û�н�һ���о�����ʵ�֡����Ľ�ʵ������Ӧ�ó�������һ����Content Provider����ʽ���ṩ���ݷ�����ڣ���һ��ͨ�����Content Provider��������Щ���ݡ����ĵ����Ӳ�������Ϊ���ķ���Content Provider��ʵ��ԭ������ʹ���龰��������ѧϰ������һ��δ�����ӿڡ�
�����е�Ӧ�ó����ǰ�����һƪ����AndroidӦ�ó������Content Provider��Ҫ���ܺ�ѧϰ�ƻ����ᵽ��һ��Ӧ�ó���ܹ���������Ƶġ����İ�������Ӧ�ó������У���һ��Ӧ�ó�������ΪArticlesProvider����ʹ����SQLite���ݿ���ά��һ��������Ϣ�б���ͬʱ���������˷������������Ϣ�б���URI�����������ǾͿ���ͨ��һ��Content Provider������������Ӧ�ó����ṩ�������������Ϣ�б��Ľӿڣ��ڶ���Ӧ�ó�������ΪArticle�����ṩ�˹���������ArticlesProviderӦ�ó����е�������Ϣ�Ľ�����ڣ������Ӧ�ó����У��û��������ӡ�ɾ��������Щ������Ϣ�����������Ǿͷֱ����������Ӧ�ó����ʵ�֡�
1. ArticlesProviderӦ�ó����ʵ��
�����Dz�����Ubuntu��ΪAndroidϵͳ����JavaӦ�ó������Application Frameworks���Ӳ������һ�ģ���packages/experimentalĿ¼�½��������ļ�Ŀ¼ArticlesProvider���ڼ����������Ӧ�ó����ʵ��֮ǰ�������Ƚ���һ�����Ӧ�ó�����������������Ϣ�����ݿ����ơ�
����֪������Androidϵͳ�У�������һ�����͵����ݿ�SQLite��SQLite��ר��ΪǶ��ʽ��Ʒ����Ƶģ�������ռ����Դ�͵��ص㣬�����ǿ�Դ�ģ��dz��ʺ���Androidƽ̨��ʹ�ã�����SQLite�ĸ�����Ϣ���Է��ʹٷ���վhttp://www.sqlite.org��
ArticlesProviderӦ�ó������ʹ��SQLite����Ϊ���ݿⱣ��������Ϣ�ģ����ݿ��ļ�����ΪArticles.db��������ֻ��һ�ű�ArticlesTable�����Ľṹ������ʾ��
-------------------------------------------------------------
| -- _id -- | -- _title -- | -- _abstrat -- | -- _url -- |
-------------------------------------------------------------
| | | | |
�����ĸ��ֶα�ʾ����һ���ֶ�_id��ʾ���µ�ID������Ϊ�Զ�������integer������Ϊ����keyֵ���ڶ����ֶ�_title��ʾ���µ���Ŀ������Ϊtext���������ֶ�_abstract��ʾ���µ�ժҪ������Ϊtext�����ĸ��ֶ�_url��ʾ���µ�URL������Ϊtext��ע�⣬�����Ǵ��㽫���ݿ����ijһ�е�������Ϊһ�������е�IDʱ����Լ����������Ϊ_id��������Ϊ���Ǿ�����Ҫ�����ݿ��л�ȡһ�����ݣ���Щ������Cursor����ʽ���أ�����Щ����������������һ����һ��ListView����ʾ�������ListView��Ҫһ������������Adapter����Ϊ����Դ����ʱ������ǾͿ��������Cursor������һ��Adapter����ЩAdapter������android.widget.CursorAdapter��������ʵ���Լ���getItemId��Ա��������ȡָ�������е�IDʱ���ͱ���Ҫ�����Cursor����Ӧ��������ȡ������Ϊ_id���ֶε����ݳ�����Ϊ��������е�ID���ظ������ߡ���Ȼ�����Dz������ݿ���ж������_id����Ҳ�ǿ��Եģ��������������ݿ��в�ѯ���ݺ�õ���Cursor�ʺ��Ծͱ���ˣ���ˣ�����������������ݿ��ʱ��������������һ��������_id�����ұ�֤��һ�е������������ݿ������Ψһ�ġ�
�������ǾͿ�ʼ�������Ӧ�ó����ʵ���ˡ����Ӧ�ó���ֻ������Դ�ļ����ֱ���Articles.java��ArticlesProvider�����Ƿ���shy.luo.providers.articles���package���档��Articles.java�ļ����棬��Ҫ�Ƕ�����һЩ������������������������Ϣ���ݵ�URI��MIME��Multipurpose Internet Mail Extensions�������Լ���ʽ�ȣ���Щ�����ǵ�����Ӧ�ó��������Щ������Ϣ����ʱҪʹ�õ��ģ���ˣ����ǰ���������һ���������ļ��У��Ժ����ǻ������������Articles.java�ļ������һ��jar�ļ���Ȼ�������Ӧ�ó���Ϳ���������������ˣ�����Ҳ������ֱ�Ӱ����Դ�����ļ���¶��������Ӧ�ó���
Դ�ļ�Articles.javaλ��src/shy/luo/providers/articlesĿ¼�£���������������ʾ��
[java] view plaincopyprint?
package shy.luo.providers.articles;
import android.net.Uri;
public class Articles {
/*Data Field*/
public static final String ID = "_id";
public static final String TITLE = "_title";
public static final String ABSTRACT = "_abstract";
public static final String URL = "_url";
/*Default sort order*/
public static final String DEFAULT_SORT_ORDER = "_id asc";
/*Call Method*/
public static final String METHOD_GET_ITEM_COUNT = "METHOD_GET_ITEM_COUNT";
public static final String KEY_ITEM_COUNT = "KEY_ITEM_COUNT";
/*Authority*/
public static final String AUTHORITY = "shy.luo.providers.articles";
/*Match Code*/
public static final int ITEM = 1;
public static final int ITEM_ID = 2;
public static final int ITEM_POS = 3;
/*MIME*/
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.shy.luo.article";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.shy.luo.article";
/*Content URI*/
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/item");
public static final Uri CONTENT_POS_URI = Uri.parse("content://" + AUTHORITY + "/pos");
}
package shy.luo.providers.articles; import android.net.Uri; public class Articles { /*Data Field*/ public static final String ID = "_id"; public static final String TITLE = "_title"; public static final String ABSTRACT = "_abstract"; public static final String URL = "_url"; /*Default sort order*/ public static final String DEFAULT_SORT_ORDER = "_id asc"; /*Call Method*/ public static final String METHOD_GET_ITEM_COUNT = "METHOD_GET_ITEM_COUNT"; public static final String KEY_ITEM_COUNT = "KEY_ITEM_COUNT"; /*Authority*/ public static final String AUTHORITY = "shy.luo.providers.articles"; /*Match Code*/ public static final int ITEM = 1; public static final int ITEM_ID = 2; public static final int ITEM_POS = 3; /*MIME*/ public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.shy.luo.article"; public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.shy.luo.article"; /*Content URI*/ public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/item"); public static final Uri CONTENT_POS_URI = Uri.parse("content://" + AUTHORITY + "/pos"); }ID��TITLE��ABSTRACT��URL�ĸ�����ǰ���Ѿ������ˣ�����������������������Ϣ�����ݱ����ĸ�������DEFAULT_SORT_ORDER�����ǵ���ContentProvider�ӿڵ�query��������ѯ����ʱ�õģ�����ʾ�Բ�ѯ�������_id�е�ֵ��С�������У�METHOD_GET_ITEM_COUNT��KEY_ITEM_COUNT���������ǵ���ContentProvider�ӿڵ�һ��δ��������call����ѯ����ʱ�õģ�����������COM�е�IDispatch�ӿڵ�Invoke������ʹ�����call����ʱ���������METHOD_GET_ITEM_COUNT��ʾ����Ҫ���������Զ����ContentProvider�����е�getItemCount��������ȡ���ݿ��е�������Ϣ��Ŀ���������������һ��Bundle����KEY_ITEM_COUNTΪ�ؼ��ֵ����С�
ʣ�µij������Ǹ�����URI��صģ������Ҫ��ϸ����һ�¡�URI��ȫ����Universal Resource Identifier����ͨ����Դ��־����ͨ��������Ψһ��־ij����Դ�������е�λ�ã����Ľṹ�����dz�����HTTP��ʽURL��һ���ģ���ʵ���ǿ��ѳ�����HTTP��ʽ��URL������URI�ṹ��һ��ʵ����URI���ڸ���һ������ϵij�����Androidϵͳ�У���Ҳ�������Լ���������Ȭij���ض���Content Provider��URI�ṹ����ͨ�����ĸ��������ɣ�������ʾ��
[content://][shy.luo.providers.articles][/item][/123]
|------A------|-----------------B-------------------|---C---|---D--|
A�����ΪScheme�����̶�Ϊcontent://����ʾ�������·������ʾ����Դ����Content Provider���ṩ�ġ�
B�����ΪAuthority����Ψһ�ر�ʶ��һ���ض���Content Provider����ˣ��ⲿ������һ��ʹ��Content Provider���ڵ�package��������ʹ������Ψһ�ġ�
C�����Ϊ��Դ·��������ʾ���������Դ�����ͣ��ⲿ�������ǿ�ѡ�ġ���������Լ���ʵ�ֵ�Content Providerֻ�ṩһ�����͵���Դ���ʣ���ô�ⲿ���ڲ��Ϳ��Ժ��ԣ���������Լ�ʵ�ֵ�Content Providerͬʱ�ṩ�˶������͵���Դ���ʣ���ô�ⲿ�����ݾͲ����Ժ����ˡ����磬���������ֵ�����Դ�����ṩ���û����ʣ�һ���DZʼDZ����ԣ�һ����ƽ����ԣ����ǾͰѷֱ����Ƕ���Ϊnotebook��pad������������һ������ϵͳ��������һ��ϸ�������ֵ�����Դ���ԱʼDZ�������˵��һ���ǰ�װ��windowsϵͳ�ģ�һ���ǰ�װ��linuxϵͳ�ģ����Ǿͷֱ�����Ƕ���Ϊnotebook/windows��notebook/linux����ƽ�������˵��һ���ǰ�װ��iosϵͳ�ģ�һ���ǰ�װ��androidϵͳ�ģ����Ǿͷֱ�����Ƕ���Ϊpad/ios��pad/android��
D�����Ϊ��ԴID������ʾ���������һ���ض�����Դ����ͨ����һ�����֣���Ӧǰ�����������ܵ����ݿ���е�_id�ֶε����ݣ���Ψһ�ر�־��ijһ����Դ�µ�һ���ض���ʵ����������ǰ��ĵ�����ԴΪ�����������������DZ��Ϊ123��װ��androidϵͳ��ƽ����ԣ����ǾͰ�������Ϊpad/android/123���������ⲿ������ʱ�����п����DZ�ʾ����ijһ����Դ�µ�����ʵ����ȡ�������ǵ�URIƥ����������ǽ����һ�������������URIƥ�����
�ص������Articles.javaԴ�ļ��У����Ƕ���������URI���ֱ���COTENT_URI��CONTENT_POS_URI������������ʾ�����ǵ�Authority�����ָ��Ϊshy.luo.providers.articles�����У�COTENT_URI������ʾ��URI��ʾ��ͨ��ID������������Ϣ�ģ���CONTENT_POS_URI������ʾ��URI��ʾ��ͨ��λ��������������Ϣ�ġ����磬content://shy.luo.providers.articles/item��ʾ�������е�������Ϣ��Ŀ��content://shy.luo.providers.articles/item/123��ʾֻ����IDֵΪ123��������Ϣ��Ŀ��content://shy.luo.providers.articles/pos/1��ʾ�������ݿ���еĵ�1��������Ϣ��Ŀ������������Ϣ��Ŀ��IDֵ��һ��Ϊ1��ͨ������CONTENT_POS_URI������������Ϣ��Ŀʱ������Ҫָ��λ�ã���Ҳ���������õ�URIƥ�������ָ���ģ��������ǽ��ῴ����
���⣬���ǻ���Ҫ������URI��Ӧ����Դ��MIME���͡�ÿ��MIME��������������ɣ�ǰ�������ݵĴ���𣬺��涨���������ࡣ��Content Provider�У�URI����Ӧ����Դ��MIME���͵Ĵ�������ͬʱ���ʵ���Դ��������Ϊ���֣����ڷ��ʵ�����Դ��URI�����Ĵ�����Ϊvnd.android.cursor.item��������ͬʱ���ʶ����Դ��URI�����Ĵ�����Ϊvnd.android.cursor.dir��Content Provider��URI����Ӧ����Դ��MIME���͵ľ���������Ҫ��Content Provider���ṩ���������ˣ����ĸ�ʽһ��Ϊvnd.[company name].[resource type]����ʽ�����磬�����ǵ������У�CONTENT_TYPE��COTENT_ITEM_TYPE���������ֱ���������MIME���ͣ����ǵĴ����ֱ�Ϊvnd.android.cursor.dir��vnd.android.cursor.item������������Ϊvdn.shy.luo.article������shy.luo���DZ�ʾ��˾���ˣ���article��ʾ��Դ������Ϊ���¡�������MIME���ͳ�����Ҫ����ʵ��ContentProvider��getType����ʱ�õ��ģ��������ǽ��ῴ����
���ITEM��ITEM_ID��POS_ID���������ֱ�������URIƥ������ƥ���롣���URI����ʽΪcontent://shy.luo.providers.articles/item����ƥ����ص�ƥ����ΪITEM�����URI����ʽΪcontent://shy.luo.providers.articles/item/#������#��ʾ����һ�����֣���ƥ����ص�ƥ����ΪITEM_ID�����URI����ʽΪ#Ҳ�DZ�ʾ����һ�����֣���ƥ����ص�ƥ����ΪITEM_POS���������������÷������ں���Ҳ���ῴ����
������Articles.java�ļ������ݾͽ������ˡ����������ٽ��Ž���λ��src/shy/luo/providers/articlesĿ¼�µ�ArticlesProvider.java�ļ�����������������ʾ��
[java] view plaincopyprint?
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
public class ArticlesProvider extends ContentProvider {
private static final String LOG_TAG = "shy.luo.providers.articles.ArticlesProvider";
private static final String DB_NAME = "Articles.db";
private static final String DB_TABLE = "ArticlesTable";
private static final int DB_VERSION = 1;
private static final String DB_CREATE = "create table " + DB_TABLE +
" (" + Articles.ID + " integer primary key autoincrement, " +
Articles.TITLE + " text not null, " +
Articles.ABSTRACT + " text not null, " +
Articles.URL + " text not null);";
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(Articles.AUTHORITY, "item", Articles.ITEM);
uriMatcher.addURI(Articles.AUTHORITY, "item/#", Articles.ITEM_ID);
uriMatcher.addURI(Articles.AUTHORITY, "pos/#", Articles.ITEM_POS);
}
private static final HashMap<String, String> articleProjectionMap;
static {
articleProjectionMap = new HashMap<String, String>();
articleProjectionMap.put(Articles.ID, Articles.ID);
articleProjectionMap.put(Articles.TITLE, Articles.TITLE);
articleProjectionMap.put(Articles.ABSTRACT, Articles.ABSTRACT);
articleProjectionMap.put(Articles.URL, Articles.URL);
}
private DBHelper dbHelper = null;
private ContentResolver resolver = null;
@Override
public boolean onCreate() {
Context context = getContext();
resolver = context.getContentResolver();
dbHelper = new DBHelper(context, DB_NAME, null, DB_VERSION);
Log.i(LOG_TAG, "Articles Provider Create");
return true;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case Articles.ITEM:
return Articles.CONTENT_TYPE;
case Articles.ITEM_ID:
case Articles.ITEM_POS:
return Articles.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Error Uri: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if(uriMatcher.match(uri) != Articles.ITEM) {
throw new IllegalArgumentException("Error Uri: " + uri);
}
SQLiteDatabase db = dbHelper.getWritableDatabase();
long id = db.insert(DB_TABLE, Articles.ID, values);
if(id < 0) {
throw new SQLiteException("Unable to insert " + values + " for " + uri);
}
Uri newUri = ContentUris.withAppendedId(uri, id);
resolver.notifyChange(newUri, null);
return newUri;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = 0;
switch(uriMatcher.match(uri)) {
case Articles.ITEM: {
count = db.update(DB_TABLE, values, selection, selectionArgs);
break;
}
case Articles.ITEM_ID: {
String id = uri.getPathSegments().get(1);
count = db.update(DB_TABLE, values, Articles.ID + "=" + id
+ (!TextUtils.isEmpty(selection) ? " and (" + selection + ')' : ""), selectionArgs);
break;
}
default:
throw new IllegalArgumentException("Error Uri: " + uri);
}
resolver.notifyChange(uri, null);
return count;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = 0;
switch(uriMatcher.match(uri)) {
case Articles.ITEM: {
count = db.delete(DB_TABLE, selection, selectionArgs);
break;
}
case Articles.ITEM_ID: {
String id = uri.getPathSegments().get(1);
count = db.delete(DB_TABLE, Articles.ID + "=" + id
+ (!TextUtils.isEmpty(selection) ? " and (" + selection + ')' : ""), selectionArgs);
break;
}
default:
throw new IllegalArgumentException("Error Uri: " + uri);
}
resolver.notifyChange(uri, null);
return count;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Log.i(LOG_TAG, "ArticlesProvider.query: " + uri);
SQLiteDatabase db = dbHelper.getReadableDatabase();
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
String limit = null;
switch (uriMatcher.match(uri)) {
case Articles.ITEM: {
sqlBuilder.setTables(DB_TABLE);
sqlBuilder.setProjectionMap(articleProjectionMap);
break;
}
case Articles.ITEM_ID: {
String id = uri.getPathSegments().get(1);
sqlBuilder.setTables(DB_TABLE);
sqlBuilder.setProjectionMap(articleProjectionMap);
sqlBuilder.appendWhere(Articles.ID + "=" + id);
break;
}
case Articles.ITEM_POS: {
String pos = uri.getPathSegments().get(1);
sqlBuilder.setTables(DB_TABLE);
sqlBuilder.setProjectionMap(articleProjectionMap);
limit = pos + ", 1";
break;
}
default:
throw new IllegalArgumentException("Error Uri: " + uri);
}
Cursor cursor = sqlBuilder.query(db, projection, selection, selectionArgs, null, null, TextUtils.isEmpty(sortOrder) ? Articles.DEFAULT_SORT_ORDER : sortOrder, limit);
cursor.setNotificationUri(resolver, uri);
return cursor;
}
@Override
public Bundle call(String method, String request, Bundle args) {
Log.i(LOG_TAG, "ArticlesProvider.call: " + method);
if(method.equals(Articles.METHOD_GET_ITEM_COUNT)) {
return getItemCount();
}
throw new IllegalArgumentException("Error method call: " + method);
}
private Bundle getItemCount() {
Log.i(LOG_TAG, "ArticlesProvider.getItemCount");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from " + DB_TABLE, null);
int count = 0;
if (cursor.moveToFirst()) {
count = cursor.getInt(0);
}
Bundle bundle = new Bundle();
bundle.putInt(Articles.KEY_ITEM_COUNT, count);
cursor.close();
db.close();
return bundle;
}
private static class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
onCreate(db);
}
}
}
import java.util.HashMap; import android.content.ContentValues; import android.content.Context; import android.content.UriMatcher; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentResolver; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; public class ArticlesProvider extends ContentProvider { private static final String LOG_TAG = "shy.luo.providers.articles.ArticlesProvider"; private static final String DB_NAME = "Articles.db"; private static final String DB_TABLE = "ArticlesTable"; private static final int DB_VERSION = 1; private static final String DB_CREATE = "create table " + DB_TABLE + " (" + Articles.ID + " integer primary key autoincrement, " + Articles.TITLE + " text not null, " + Articles.ABSTRACT + " text not null, " + Articles.URL + " text not null);"; private static final UriMatcher uriMatcher; static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(Articles.AUTHORITY, "item", Articles.ITEM); uriMatcher.addURI(Articles.AUTHORITY, "item/#", Articles.ITEM_ID); uriMatcher.addURI(Articles.AUTHORITY, "pos/#", Articles.ITEM_POS); } private static final HashMap<String, String> articleProjectionMap; static { articleProjectionMap = new HashMap<String, String>(); articleProjectionMap.put(Articles.ID, Articles.ID); articleProjectionMap.put(Articles.TITLE, Articles.TITLE); articleProjectionMap.put(Articles.ABSTRACT, Articles.ABSTRACT); articleProjectionMap.put(Articles.URL, Articles.URL); } private DBHelper dbHelper = null; private ContentResolver resolver = null; @Override public boolean onCreate() { Context context = getContext(); resolver = context.getContentResolver(); dbHelper = new DBHelper(context, DB_NAME, null, DB_VERSION); Log.i(LOG_TAG, "Articles Provider Create"); return true; } @Override public String getType(Uri uri) { switch (uriMatcher.match(uri)) { case Articles.ITEM: return Articles.CONTENT_TYPE; case Articles.ITEM_ID: case Articles.ITEM_POS: return Articles.CONTENT_ITEM_TYPE; default: throw new IllegalArgumentException("Error Uri: " + uri); } } @Override public Uri insert(Uri uri, ContentValues values) { if(uriMatcher.match(uri) != Articles.ITEM) { throw new IllegalArgumentException("Error Uri: " + uri); } SQLiteDatabase db = dbHelper.getWritableDatabase(); long id = db.insert(DB_TABLE, Articles.ID, values); if(id < 0) { throw new SQLiteException("Unable to insert " + values + " for " + uri); } Uri newUri = ContentUris.withAppendedId(uri, id); resolver.notifyChange(newUri, null); return newUri; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count = 0; switch(uriMatcher.match(uri)) { case Articles.ITEM: { count = db.update(DB_TABLE, values, selection, selectionArgs); break; } case Articles.ITEM_ID: { String id = uri.getPathSegments().get(1); count = db.update(DB_TABLE, values, Articles.ID + "=" + id + (!TextUtils.isEmpty(selection) ? " and (" + selection + ')' : ""), selectionArgs); break; } default: throw new IllegalArgumentException("Error Uri: " + uri); } resolver.notifyChange(uri, null); return count; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int count = 0; switch(uriMatcher.match(uri)) { case Articles.ITEM: { count = db.delete(DB_TABLE, selection, selectionArgs); break; } case Articles.ITEM_ID: { String id = uri.getPathSegments().get(1); count = db.delete(DB_TABLE, Articles.ID + "=" + id + (!TextUtils.isEmpty(selection) ? " and (" + selection + ')' : ""), selectionArgs); break; } default: throw new IllegalArgumentException("Error Uri: " + uri); } resolver.notifyChange(uri, null); return count; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Log.i(LOG_TAG, "ArticlesProvider.query: " + uri); SQLiteDatabase db = dbHelper.getReadableDatabase(); SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder(); String limit = null; switch (uriMatcher.match(uri)) { case Articles.ITEM: { sqlBuilder.setTables(DB_TABLE); sqlBuilder.setProjectionMap(articleProjectionMap); break; } case Articles.ITEM_ID: { String id = uri.getPathSegments().get(1); sqlBuilder.setTables(DB_TABLE); sqlBuilder.setProjectionMap(articleProjectionMap); sqlBuilder.appendWhere(Articles.ID + "=" + id); break; } case Articles.ITEM_POS: { String pos = uri.getPathSegments().get(1); sqlBuilder.setTables(DB_TABLE); sqlBuilder.setProjectionMap(articleProjectionMap); limit = pos + ", 1"; break; } default: throw new IllegalArgumentException("Error Uri: " + uri); } Cursor cursor = sqlBuilder.query(db, projection, selection, selectionArgs, null, null, TextUtils.isEmpty(sortOrder) ? Articles.DEFAULT_SORT_ORDER : sortOrder, limit); cursor.setNotificationUri(resolver, uri); return cursor; } @Override public Bundle call(String method, String request, Bundle args) { Log.i(LOG_TAG, "ArticlesProvider.call: " + method); if(method.equals(Articles.METHOD_GET_ITEM_COUNT)) { return getItemCount(); } throw new IllegalArgumentException("Error method call: " + method); } private Bundle getItemCount() { Log.i(LOG_TAG, "ArticlesProvider.getItemCount"); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select count(*) from " + DB_TABLE, null); int count = 0; if (cursor.moveToFirst()) { count = cursor.getInt(0); } Bundle bundle = new Bundle(); bundle.putInt(Articles.KEY_ITEM_COUNT, count); cursor.close(); db.close(); return bundle; } private static class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DB_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE); onCreate(db); } } }
������ʵ���Լ���Content Providerʱ������̳���ContentProvider�࣬����ʵ����������������
-- onCreate()������ִ��һЩ��ʼ���Ĺ�����
-- query(Uri, String[], String, String[], String)�������������ݸ������ߡ�
-- insert(Uri, ContentValues)�����������µ����ݡ�
-- update(Uri, ContentValues, String, String[])�������������е����ݡ�
-- delete(Uri, String, String[])������ɾ�����ݡ�
-- getType(Uri)�������������ݵ�MIME���͡�
��Щ������ʵ�ֶ��Ƚϼ��������ǾͲ���ϸ�����ˣ���Ҫ�������Ҫ�㡣
��һ����������ArticlesProvider����ڲ��ж�����һ��DBHelper�࣬���̳���SQLiteOpenHelper�࣬�������ø������Dz������ݿ�ġ�ʹ�����DBHelper���������������ݿ�ĺô���ֻ�е����ǵ�һ�ζ����ݿ�ʱ�в���ʱ��ϵͳ�Ż�ִ�д����ݿ��ļ��IJ��������������������˵��ֻ�е�����Ӧ�ó����һ�ε���query��insert��update����delete�������������ݿ�ʱ�����DzŻ�����ȥ����Ӧ�����ݿ��ļ���������onCreate������Ͳ���ִ�д����ݿ�IJ�������Ϊ����һ����ʱ�IJ���������onCreate�����У�Ҫ����ִ����Щ��ʱ�IJ�����
�ڶ���������URIƥ�������Ϊ�����Ǹ���URI���������ݿ�ģ���ͬ��URI��Ӧ��ͬ�IJ�����������������Ҫ�����URIƥ����������������ǻ��һ��URIʱ�����ܿ��ٵ��жϳ�Ҫ���ȥ�������ݿ⡣����URIƥ�����Ĵ���������ʾ��
[java] view plaincopyprint?
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(Articles.AUTHORITY, "item", Articles.ITEM);
uriMatcher.addURI(Articles.AUTHORITY, "item/#", Articles.ITEM_ID);
uriMatcher.addURI(Articles.AUTHORITY, "pos/#", Articles.ITEM_POS);
}
private static final UriMatcher uriMatcher; static { uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher.addURI(Articles.AUTHORITY, "item", Articles.ITEM); uriMatcher.addURI(Articles.AUTHORITY, "item/#", Articles.ITEM_ID); uriMatcher.addURI(Articles.AUTHORITY, "pos/#", Articles.ITEM_POS); }
�ڴ���UriMatcher����uriMatcherʱ�����Ǵ������캯���IJ���ΪUriMatcher.NO_MATCH������ʾ��uriMatcher����ƥ��ָ����URIʱ���ͷ��ش���UriMatcher.NO_MATCH������������������ƥ����ֱ���content://shy.luo.providers.articles/item��content://shy.luo.providers.articles/item/#��content://shy.luo.providers.articles/pos/#�����ǵ�ƥ����ֱ���Articles.ITEM��Articles.ITEM_ID��Articles.ITEM_POS�����У�����#��ʾƥ���κ����֡�
��������SQLiteQueryBuilder��ʹ�á���query�����У�����ʹ��SQLiteQueryBuilder���������ݿ��ѯ������ʹ�������ĺô������ǿ��Բ������ݿ�����ֶα�¶�����������ṩ������������Ӧ�ó���ʹ�ã������Ϳ������ݿ���ڲ�����������������������չ��ά�����б�������ʵ������ӳ�������������HashMap��Ա������ʵ�ֵģ�
[java] view plaincopyprint?
private static final HashMap<String, String> articleProjectionMap;
static {
articleProjectionMap = new HashMap<String, String>();
articleProjectionMap.put(Articles.ID, Articles.ID);
articleProjectionMap.put(Articles.TITLE, Articles.TITLE);
articleProjectionMap.put(Articles.ABSTRACT, Articles.ABSTRACT);
articleProjectionMap.put(Articles.URL, Articles.URL);
}
private static final HashMap<String, String> articleProjectionMap; static { articleProjectionMap = new HashMap<String, String>(); articleProjectionMap.put(Articles.ID, Articles.ID); articleProjectionMap.put(Articles.TITLE, Articles.TITLE); articleProjectionMap.put(Articles.ABSTRACT, Articles.ABSTRACT); articleProjectionMap.put(Articles.URL, Articles.URL); }�������put�����У���һ��������ʾ�еı������ڶ���������ʾ�е���ʵ���ơ�����������У����ǰ��еı����ͺ���ʵ���ƶ����ó�һ���ġ�
���ĵ������ݸ��»��Ƶ�ʹ�á�ִ��insert��update��delete��������ʱ�����ᵼ�����ݿ��е����ݷ����仯��������ʱ��Ҫͨ������ContentResolver�ӿڵ�notifyChange������֪ͨ��Щע���˼���ض�URI��ContentObserver����ʹ�����ǿ�����Ӧ��ִ��һЩ������������������ڽ����ϵ���ʾ����query�����У����շ��ظ������ߵ���һ��Cursor������������Cursor�Ժ���ͨ������deleteRow����commitUpdates��ִ��һЩ�������ݿ�IJ�������ʱ��ҲҪ֪ͨ��Щע������Ӧ��URI��ContentObserver������Ӧ�Ĵ�������ˣ������ڷ���Cursor֮ǰ��Ҫͨ��Cursor���setNotificationUri�������ѵ�ǰ�����ĵ�ContentResolver���浽Curosr����ȥ���Ա㵱ͨ�����Cursor���ı����ݿ��е�����ʱ������֪ͨ��Ӧ��ContentObserver�����������������÷��Ѿ���ʱ�ˣ���������ͨ�����Cursor���ı����ݿ�����ݣ�Ҫ��Cursor�е����ݿ�����ֻ�����ݡ��������Cursor���setNotificationUri������������һ�����ã�����ע����ĵڶ�������uri����Ӧ����Cursor�е����ݣ��������uri����Cursorʱ��Cursor�ͻ�ע���Լ���ContentObserver��������uri��Ӧ�����ݵı仯��һ�����uri��Ӧ�����ݷ����仯�����Cursor��Ӧ�����ݾͲ������µ��ˣ���ʱ�����Ҫ��ȡһЩ���������������ˡ�
���������ʵ����ContentProvider��call���������������һ��δ�����ĺ�����������Ӧ�ó���ֻ��AndroidԴ���뻷���¿���������ʹ�����������������������Ŀ����ʲô�أ�����֪������������Ҫ��Content Provider�л������ʱ��һ�㶼��Ҫͨ����������query��������õģ���������������ݷ���Cursor�������ظ������ߡ���ǰ��һƪ����AndroidӦ�ó������Content Provider��Ҫ���ܺ�ѧϰ�ƻ��У������ᵽ��Content Provider����������Ӧ�ó�������ݣ���ͨ�����������ڴ�������ġ���Ҫ��������������ʱ��ʹ�����������ڴ��������������кô��ģ������Լ������ݵĿ�������ߴ���Ч�ʡ����ǣ���Ҫ�����������Сʱ��ʹ�����������ڴ�����Ϊý����е���ţ����ɱ����ζ���ˣ���Ϊ���������ڴ沢������ѵ���ͣ�ϵͳ���������������ڴ�Ҳ���п����ġ���ˣ�Content Provider�ṩ��call�������õ�����Ӧ�ó�������ȡһЩ�Զ������ݣ���Щ����һ�㶼�Ƚ�С�����磬ֻ�Ǵ���һ�������������Ϳ����ý�С�Ĵ������ﵽ��ͬ�����ݴ����Ŀ�ġ�
���ˣ�ArticlesProvider��Դ����ͷ������ˣ��������ǻ�Ҫ��AndroidManifest.xml�ļ����������ArticlesProvider���������ʹ�á�AndroidManifest.xml�ļ�������������ʾ��
[html] view plaincopyprint?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shy.luo.providers.articles">
<application android:process="shy.luo.process.article"
android:label="@string/app_label"
android:icon="@drawable/app_icon">
<provider android:name="ArticlesProvider"
android:authorities="shy.luo.providers.articles"
android:label="@string/provider_label"
android:multiprocess="false">
</provider>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="shy.luo.providers.articles"> <application android:process="shy.luo.process.article" android:label="@string/app_label" android:icon="@drawable/app_icon"> <provider android:name="ArticlesProvider" android:authorities="shy.luo.providers.articles" android:label="@string/provider_label" android:multiprocess="false"> </provider> </application> </manifest>������Content Provider��ʱ������Ҫ�ľ���Ҫָ������authorities�����ˣ�ֻ��������������ԣ�������Ӧ�ó������ͨ�������ҵ����Content Provider����Ҫ��Ҫע��ģ��������õ�authorities���Ե�ֵ�Ǻ�����ǰ����Articles.java�ļ��ж����AUTHORITY������ֵ��һ�µġ�����һ������multiprocess��һ������ֵ������ʾ���Content Provider�Ƿ������ÿ���ͻ������д���һ��ʵ������������Ŀ����Ϊ�˼��ٽ��̼�ͨ�ŵĿ�������������Ϊ�˼��ٲ���Ҫ���ڴ濪����������multiprocess��ֵ����Ϊfalse��ʹ��ϵͳֻ����һ��Content Providerʵ�����ڣ����������Լ��Ľ����С�����������ļ����棬���ǻ������������Content Provider�ķ���Ȩ�ޣ���������Ϊ�˼�������Ͳ�����Ȩ���ˡ��й�Content Provider�ķ���Ȩ�����ã����Բο��ٷ��ĵ�http://developer.android.com/guide/topics/manifest/provider-element.html��
���Ӧ�ó���ʹ�õ����ַ�����Դ������res/values/strings.xml�ļ��У���������������ʾ��
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_label">Articles Storage</string>
<string name="provider_label">Articles</string>
</resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_label">Articles Storage</string> <string name="provider_label">Articles</string> </resources>����Content Provider���͵�Ӧ�ó�����û���û�����ģ���ˣ����Dz���Ҫ��res/layoutĿ¼��Ϊ���������������ļ���
����ı���ű�Android.mk������������ʾ��
[html] view plaincopyprint?
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := ArticlesProvider
include $(BUILD_PACKAGE)
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-subdir-java-files) LOCAL_PACKAGE_NAME := ArticlesProvider include $(BUILD_PACKAGE)�������ǾͿ��Բ�����ε�������AndroidԴ�����е�ģ��һ��������ʹ�����Ӧ�ó����ˣ�
[html] view plaincopyprint?
USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/ArticlesProvider
USER-NAME@MACHINE-NAME:~/Android$ make snod
USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/ArticlesProvider USER-NAME@MACHINE-NAME:~/Android$ make snod
����������õ�Androidϵͳ�����ļ�system.img�Ͱ�������������������ArticlesProviderӦ�ó����ˡ�
ǰ��˵������Articles.java�ļ��ж���ij�����Ҫ��������Ӧ�ó���ʹ�õģ���ô�����Dz���ֱ�Ӱ����Դ�ļ������������أ��������Ե�̫��רҵ�ˣ��������õ�����ļ�������Ҫ����shy/luo/providers/articlesĿ¼�»���Ҫ�����Articles�����ڵ�package�ĵ���������ʹ�á���ȷ�������ǰѱ���õ�Articles.java�ļ������һ��jar�ļ�����������ʹ�á�����ArticlesProvider���Ӧ�ó���ɹ������ɵ��м��ļ�����out/target/common/obj/APPS/ArticlesProvider_intermediatesĿ¼�£����ǽ��뵽���Ŀ¼�У�Ȼ��ִ������������Articles.class�ļ���ȡ������
[html] view plaincopyprint?
USER-NAME@MACHINE-NAME:~/Android/out/target/common/obj/APPS/ArticlesProvider_intermediates$ jar -xvf classes.jar shy/luo/providers/articles/Articles.class
USER-NAME@MACHINE-NAME:~/Android/out/target/common/obj/APPS/ArticlesProvider_intermediates$ jar -xvf classes.jar shy/luo/providers/articles/Articles.class
Ȼ���ٵ���������Articles.class�ļ���
[html] view plaincopyprint?
USER-NAME@MACHINE-NAME:~/Android/out/target/common/obj/APPS/ArticlesProvider_intermediates$ jar -cvf ArticlesProvider.jar ./shy
USER-NAME@MACHINE-NAME:~/Android/out/target/common/obj/APPS/ArticlesProvider_intermediates$ jar -cvf ArticlesProvider.jar ./shy���������ǵõ���ArticlesProvider.jar�ļ��Ͱ�����Articles.java����ļ��ж���ij����ˣ��������õ�����ļ��Ϳ��Կ����Լ���Ӧ�ó���������������ArticlesProvider���Content Provider�б���������ˡ����������Ǿͽ��ܵ������ArticlesProvider����ȡ���ݵĵ�����Ӧ�ó���Article��
2. ArticleӦ�ó����ʵ��
�����Dz���ǰ���ArticlesProvider���̣���packages/experimentalĿ¼�½��������ļ�Ŀ¼Article�����Ӧ�ó������������������ArticlesProviderӦ�ó����б����������Ϣ�ģ���ˣ�����Ҫ�����Ӧ��Content Provider�ӿ�������ArticlesProvider�е����ݡ����������ڹ���Ŀ¼Article���洴��һ��libsĿ¼��������õ���ArticlesProvider.jar����libsĿ¼�£����������ڱ���ű���ʱ���ٰ������õ������������������ǾͿ�ʼ�������Ӧ�ó����ʵ�֡�
���Ӧ�ó����������MainActivity������һ��ListView�ؼ���������ʾ��ArticlesProvider�еõ���������Ϣ��Ŀ��������������ϣ�������������ӡ�ɾ������������Ϣ������Ҫ���ӡ�ɾ�����߸���������Ϣʱ���ͻ���������һ������ArticleActivity��ȥִ�о���IJ�����Ϊ�˷��㿪�������ǰ�ÿһ��������Ϣ��Ŀ��װ����һ��Article�࣬���Ұ���ArticlesProvider�������IJ�����ͨ��ArticlesAdapter����ʵ�֡��������ÿһ����ľ߱�ʵ�֡�
������Article���ʵ�֣���ʵ����src/shy/luo/Article.java�ļ��У�
[java] view plaincopyprint?
package shy.luo.article;
public class Article {
private int id;
private String title;
private String abs;
private String url;
public Article(int id, String title, String abs, String url) {
this.id = id;
this.title = title;
this.abs = abs;
this.url = url;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
public void setAbstract(String abs) {
this.abs = abs;
}
public String getAbstract() {
return this.abs;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return this.url;
}
}
package shy.luo.article; public class Article { private int id; private String title; private String abs; private String url; public Article(int id, String title, String abs, String url) { this.id = id; this.title = title; this.abs = abs; this.url = url; } public void setId(int id) { this.id = id; } public int getId() { return this.id; } public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } public void setAbstract(String abs) { this.abs = abs; } public String getAbstract() { return this.abs; } public void setUrl(String url) { this.url = url; } public String getUrl() { return this.url; } }������ArticlesAdapter���ʵ�֣���ʵ����src/shy/luo/ArticlesAdapter.java�ļ��У�
[java] view plaincopyprint?
package shy.luo.article;
import java.util.LinkedList;
import shy.luo.providers.articles.Articles;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.IContentProvider;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
public class ArticlesAdapter {
private static final String LOG_TAG = "shy.luo.article.ArticlesAdapter";
private ContentResolver resolver = null;
public ArticlesAdapter(Context context) {
resolver = context.getContentResolver();
}
public long insertArticle(Article article) {
ContentValues values = new ContentValues();
values.put(Articles.TITLE, article.getTitle());
values.put(Articles.ABSTRACT, article.getAbstract());
values.put(Articles.URL, article.getUrl());
Uri uri = resolver.insert(Articles.CONTENT_URI, values);
String itemId = uri.getPathSegments().get(1);
return Integer.valueOf(itemId).longValue();
}
public boolean updateArticle(Article article) {
Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, article.getId());
ContentValues values = new ContentValues();
values.put(Articles.TITLE, article.getTitle());
values.put(Articles.ABSTRACT, article.getAbstract());
values.put(Articles.URL, article.getUrl());
int count = resolver.update(uri, values, null, null);
return count > 0;
}
public boolean removeArticle(int id) {
Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, id);
int count = resolver.delete(uri, null, null);
return count > 0;
}
public LinkedList<Article> getAllArticles() {
LinkedList<Article> articles = new LinkedList<Article>();
String[] projection = new String[] {
Articles.ID,
Articles.TITLE,
Articles.ABSTRACT,
Articles.URL
};
Cursor cursor = resolver.query(Articles.CONTENT_URI, projection, null, null, Articles.DEFAULT_SORT_ORDER);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(0);
String title = cursor.getString(1);
String abs = cursor.getString(2);
String url = cursor.getString(3);
Article article = new Article(id, title, abs, url);
articles.add(article);
} while(cursor.moveToNext());
}
return articles;
}
public int getArticleCount() {
int count = 0;
try {
IContentProvider provider = resolver.acquireProvider(Articles.CONTENT_URI);
Bundle bundle = provider.call(Articles.METHOD_GET_ITEM_COUNT, null, null);
count = bundle.getInt(Articles.KEY_ITEM_COUNT, 0);
} catch(RemoteException e) {
e.printStackTrace();
}
return count;
}
public Article getArticleById(int id) {
Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, id);
String[] projection = new String[] {
Articles.ID,
Articles.TITLE,
Articles.ABSTRACT,
Articles.URL
};
Cursor cursor = resolver.query(uri, projection, null, null, Articles.DEFAULT_SORT_ORDER);
Log.i(LOG_TAG, "cursor.moveToFirst");
if (!cursor.moveToFirst()) {
return null;
}
String title = cursor.getString(1);
String abs = cursor.getString(2);
String url = cursor.getString(3);
return new Article(id, title, abs, url);
}
public Article getArticleByPos(int pos) {
Uri uri = ContentUris.withAppendedId(Articles.CONTENT_POS_URI, pos);
String[] projection = new String[] {
Articles.ID,
Articles.TITLE,
Articles.ABSTRACT,
Articles.URL
};
Cursor cursor = resolver.query(uri, projection, null, null, Articles.DEFAULT_SORT_ORDER);
if (!cursor.moveToFirst()) {
return null;
}
int id = cursor.getInt(0);
String title = cursor.getString(1);
String abs = cursor.getString(2);
String url = cursor.getString(3);
return new Article(id, title, abs, url);
}
}
package shy.luo.article; import java.util.LinkedList; import shy.luo.providers.articles.Articles; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.IContentProvider; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.RemoteException; import android.util.Log; public class ArticlesAdapter { private static final String LOG_TAG = "shy.luo.article.ArticlesAdapter"; private ContentResolver resolver = null; public ArticlesAdapter(Context context) { resolver = context.getContentResolver(); } public long insertArticle(Article article) { ContentValues values = new ContentValues(); values.put(Articles.TITLE, article.getTitle()); values.put(Articles.ABSTRACT, article.getAbstract()); values.put(Articles.URL, article.getUrl()); Uri uri = resolver.insert(Articles.CONTENT_URI, values); String itemId = uri.getPathSegments().get(1); return Integer.valueOf(itemId).longValue(); } public boolean updateArticle(Article article) { Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, article.getId()); ContentValues values = new ContentValues(); values.put(Articles.TITLE, article.getTitle()); values.put(Articles.ABSTRACT, article.getAbstract()); values.put(Articles.URL, article.getUrl()); int count = resolver.update(uri, values, null, null); return count > 0; } public boolean removeArticle(int id) { Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, id); int count = resolver.delete(uri, null, null); return count > 0; } public LinkedList<Article> getAllArticles() { LinkedList<Article> articles = new LinkedList<Article>(); String[] projection = new String[] { Articles.ID, Articles.TITLE, Articles.ABSTRACT, Articles.URL }; Cursor cursor = resolver.query(Articles.CONTENT_URI, projection, null, null, Articles.DEFAULT_SORT_ORDER); if (cursor.moveToFirst()) { do { int id = cursor.getInt(0); String title = cursor.getString(1); String abs = cursor.getString(2); String url = cursor.getString(3); Article article = new Article(id, title, abs, url); articles.add(article); } while(cursor.moveToNext()); } return articles; } public int getArticleCount() { int count = 0; try { IContentProvider provider = resolver.acquireProvider(Articles.CONTENT_URI); Bundle bundle = provider.call(Articles.METHOD_GET_ITEM_COUNT, null, null); count = bundle.getInt(Articles.KEY_ITEM_COUNT, 0); } catch(RemoteException e) { e.printStackTrace(); } return count; } public Article getArticleById(int id) { Uri uri = ContentUris.withAppendedId(Articles.CONTENT_URI, id); String[] projection = new String[] { Articles.ID, Articles.TITLE, Articles.ABSTRACT, Articles.URL }; Cursor cursor = resolver.query(uri, projection, null, null, Articles.DEFAULT_SORT_ORDER); Log.i(LOG_TAG, "cursor.moveToFirst"); if (!cursor.moveToFirst()) { return null; } String title = cursor.getString(1); String abs = cursor.getString(2); String url = cursor.getString(3); return new Article(id, title, abs, url); } public Article getArticleByPos(int pos) { Uri uri = ContentUris.withAppendedId(Articles.CONTENT_POS_URI, pos); String[] projection = new String[] { Articles.ID, Articles.TITLE, Articles.ABSTRACT, Articles.URL }; Cursor cursor = resolver.query(uri, projection, null, null, Articles.DEFAULT_SORT_ORDER); if (!cursor.moveToFirst()) { return null; } int id = cursor.getInt(0); String title = cursor.getString(1); String abs = cursor.getString(2); String url = cursor.getString(3); return new Article(id, title, abs, url); } }����������ڹ��캯��������Ӧ�ó��������ĵ�ContentResolver�ӿڣ�Ȼ��ͨ���Ϳ���ͨ������ӿ�������ArticlesProvider�е�������Ϣ�ˡ���Ա����insertArticle��updateArticle��removeArticle�ֱ��������������º�ɾ��һ��������Ϣ��Ŀ����Ա����getAllArticlese������ȡ���е�������Ϣ����Ա����getArticleById��getArticleByPos�ֱ�������µ�ID��λ������þ���������Ϣ��Ŀ����Ա����getArticleCountֱ��ʹ��ContentProvider��δ�����ӿ�call�����������Ϣ��Ŀ��������ע�⣬�������ҪԴ���뻷���±������ͨ����
�����dz���������MainActivity���ʵ�֣���ʵ����src/shy/luo/article/MainActivity.java�ļ��У�
[java] view plaincopyprint?
package shy.luo.article;
import shy.luo.providers.articles.Articles;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
private final static String LOG_TAG = "shy.luo.article.MainActivity";
private final static int ADD_ARTICAL_ACTIVITY = 1;
private final static int EDIT_ARTICAL_ACTIVITY = 2;
private ArticlesAdapter aa = null;
private ArticleAdapter adapter = null;
private ArticleObserver observer = null;
private ListView articleList = null;
private Button addButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aa = new ArticlesAdapter(this);
articleList = (ListView)findViewById(R.id.listview_article);
adapter = new ArticleAdapter(this);
articleList.setAdapter(adapter);
articleList.setOnItemClickListener(this);
observer = new ArticleObserver(new Handler());
getContentResolver().registerContentObserver(Articles.CONTENT_URI, true, observer);
addButton = (Button)findViewById(R.id.button_add);
addButton.setOnClickListener(this);
Log.i(LOG_TAG, "MainActivity Created");
}
@Override
public void onDestroy() {
super.onDestroy();
getContentResolver().unregisterContentObserver(observer);
}
@Override
public void onClick(View v) {
if(v.equals(addButton)) {
Intent intent = new Intent(this, ArticleActivity.class);
startActivityForResult(intent, ADD_ARTICAL_ACTIVITY);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Intent intent = new Intent(this, ArticleActivity.class);
Article article = aa.getArticleByPos(pos);
intent.putExtra(Articles.ID, article.getId());
intent.putExtra(Articles.TITLE, article.getTitle());
intent.putExtra(Articles.ABSTRACT, article.getAbstract());
intent.putExtra(Articles.URL, article.getUrl());
startActivityForResult(intent, EDIT_ARTICAL_ACTIVITY);
}
@Override
public void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case ADD_ARTICAL_ACTIVITY: {
if(resultCode == Activity.RESULT_OK) {
String title = data.getStringExtra(Articles.TITLE);
String abs = data.getStringExtra(Articles.ABSTRACT);
String url = data.getStringExtra(Articles.URL);
Article article = new Article(-1, title, abs, url);
aa.insertArticle(article);
}
break;
}
case EDIT_ARTICAL_ACTIVITY: {
if(resultCode == Activity.RESULT_OK) {
int action = data.getIntExtra(ArticleActivity.EDIT_ARTICLE_ACTION, -1);
if(action == ArticleActivity.MODIFY_ARTICLE) {
int id = data.getIntExtra(Articles.ID, -1);
String title = data.getStringExtra(Articles.TITLE);
String abs = data.getStringExtra(Articles.ABSTRACT);
String url = data.getStringExtra(Articles.URL);
Article article = new Article(id, title, abs, url);
aa.updateArticle(article);
} else if(action == ArticleActivity.DELETE_ARTICLE) {
int id = data.getIntExtra(Articles.ID, -1);
aa.removeArticle(id);
}
}
break;
}
}
}
private class ArticleObserver extends ContentObserver {
public ArticleObserver(Handler handler) {
super(handler);
}
@Override
public void onChange (boolean selfChange) {
adapter.notifyDataSetChanged();
}
}
private class ArticleAdapter extends BaseAdapter {
private LayoutInflater inflater;
public ArticleAdapter(Context context){
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return aa.getArticleCount();
}
@Override
public Object getItem(int pos) {
return aa.getArticleByPos(pos);
}
@Override
public long getItemId(int pos) {
return aa.getArticleByPos(pos).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Article article = (Article)getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, null);
}
TextView titleView = (TextView)convertView.findViewById(R.id.textview_article_title);
titleView.setText("Title: " + article.getTitle());
TextView abstractView = (TextView)convertView.findViewById(R.id.textview_article_abstract);
abstractView.setText("Abstract: " + article.getAbstract());
TextView urlView = (TextView)convertView.findViewById(R.id.textview_article_url);
urlView.setText("URL: " + article.getUrl());
return convertView;
}
}
}
package shy.luo.article; import shy.luo.providers.articles.Articles; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.ContentObserver; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener { private final static String LOG_TAG = "shy.luo.article.MainActivity"; private final static int ADD_ARTICAL_ACTIVITY = 1; private final static int EDIT_ARTICAL_ACTIVITY = 2; private ArticlesAdapter aa = null; private ArticleAdapter adapter = null; private ArticleObserver observer = null; private ListView articleList = null; private Button addButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); aa = new ArticlesAdapter(this); articleList = (ListView)findViewById(R.id.listview_article); adapter = new ArticleAdapter(this); articleList.setAdapter(adapter); articleList.setOnItemClickListener(this); observer = new ArticleObserver(new Handler()); getContentResolver().registerContentObserver(Articles.CONTENT_URI, true, observer); addButton = (Button)findViewById(R.id.button_add); addButton.setOnClickListener(this); Log.i(LOG_TAG, "MainActivity Created"); } @Override public void onDestroy() { super.onDestroy(); getContentResolver().unregisterContentObserver(observer); } @Override public void onClick(View v) { if(v.equals(addButton)) { Intent intent = new Intent(this, ArticleActivity.class); startActivityForResult(intent, ADD_ARTICAL_ACTIVITY); } } @Override public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { Intent intent = new Intent(this, ArticleActivity.class); Article article = aa.getArticleByPos(pos); intent.putExtra(Articles.ID, article.getId()); intent.putExtra(Articles.TITLE, article.getTitle()); intent.putExtra(Articles.ABSTRACT, article.getAbstract()); intent.putExtra(Articles.URL, article.getUrl()); startActivityForResult(intent, EDIT_ARTICAL_ACTIVITY); } @Override public void onActivityResult(int requestCode,int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case ADD_ARTICAL_ACTIVITY: { if(resultCode == Activity.RESULT_OK) { String title = data.getStringExtra(Articles.TITLE); String abs = data.getStringExtra(Articles.ABSTRACT); String url = data.getStringExtra(Articles.URL); Article article = new Article(-1, title, abs, url); aa.insertArticle(article); } break; } case EDIT_ARTICAL_ACTIVITY: { if(resultCode == Activity.RESULT_OK) { int action = data.getIntExtra(ArticleActivity.EDIT_ARTICLE_ACTION, -1); if(action == ArticleActivity.MODIFY_ARTICLE) { int id = data.getIntExtra(Articles.ID, -1); String title = data.getStringExtra(Articles.TITLE); String abs = data.getStringExtra(Articles.ABSTRACT); String url = data.getStringExtra(Articles.URL); Article article = new Article(id, title, abs, url); aa.updateArticle(article); } else if(action == ArticleActivity.DELETE_ARTICLE) { int id = data.getIntExtra(Articles.ID, -1); aa.removeArticle(id); } } break; } } } private class ArticleObserver extends ContentObserver { public ArticleObserver(Handler handler) { super(handler); } @Override public void onChange (boolean selfChange) { adapter.notifyDataSetChanged(); } } private class ArticleAdapter extends BaseAdapter { private LayoutInflater inflater; public ArticleAdapter(Context context){ inflater = LayoutInflater.from(context); } @Override public int getCount() { return aa.getArticleCount(); } @Override public Object getItem(int pos) { return aa.getArticleByPos(pos); } @Override public long getItemId(int pos) { return aa.getArticleByPos(pos).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { Article article = (Article)getItem(position); if (convertView == null) { convertView = inflater.inflate(R.layout.item, null); } TextView titleView = (TextView)convertView.findViewById(R.id.textview_article_title); titleView.setText("Title: " + article.getTitle()); TextView abstractView = (TextView)convertView.findViewById(R.id.textview_article_abstract); abstractView.setText("Abstract: " + article.getAbstract()); TextView urlView = (TextView)convertView.findViewById(R.id.textview_article_url); urlView.setText("URL: " + article.getUrl()); return convertView; } } }
��Ӧ�ó�����������У�����ʹ��һ��ListView����ʾ������Ϣ��Ŀ�����ListView������Դ��ArticleAdapter�����ṩ����ArticleAdapter������ͨ��ArticlesAdapter�������ArticlesProvider�е�������Ϣ�ġ���MainActivity��onCreate���������ǻ�ͨ��Ӧ�ó��������ĵ�ContentResolver�ӿ���ע����һ��ArticleObserver���������ArticlesProvider�е�������Ϣ��һ��ArticlesProvider�е�������Ϣ�����仯���ͻ�ͨ��ArticleAdapter����ʵʱ����ListView�е�������Ϣ��
������ArticleActivity���ʵ�֣���ʵ����src/shy/luo/article/ArticleActivity.java�ļ��У�
[java] view plaincopyprint?
package shy.luo.article;
import shy.luo.providers.articles.Articles;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ArticleActivity extends Activity implements View.OnClickListener {
private final static String LOG_TAG = "shy.luo.article.ArticleActivity";
public final static String EDIT_ARTICLE_ACTION = "EDIT_ARTICLE_ACTION";
public final static int MODIFY_ARTICLE = 1;
public final static int DELETE_ARTICLE = 2;
private int articleId = -1;
private EditText titleEdit = null;
private EditText abstractEdit = null;
private EditText urlEdit = null;
private Button addButton = null;
private Button modifyButton = null;
private Button deleteButton = null;
private Button cancelButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article);
titleEdit = (EditText)findViewById(R.id.edit_article_title);
abstractEdit = (EditText)findViewById(R.id.edit_article_abstract);
urlEdit = (EditText)findViewById(R.id.edit_article_url);
addButton = (Button)findViewById(R.id.button_add_article);
addButton.setOnClickListener(this);
modifyButton = (Button)findViewById(R.id.button_modify);
modifyButton.setOnClickListener(this);
deleteButton = (Button)findViewById(R.id.button_delete);
deleteButton.setOnClickListener(this);
cancelButton = (Button)findViewById(R.id.button_cancel);
cancelButton.setOnClickListener(this);
Intent intent = getIntent();
articleId = intent.getIntExtra(Articles.ID, -1);
if(articleId != -1) {
String title = intent.getStringExtra(Articles.TITLE);
titleEdit.setText(title);
String abs = intent.getStringExtra(Articles.ABSTRACT);
abstractEdit.setText(abs);
String url = intent.getStringExtra(Articles.URL);
urlEdit.setText(url);
addButton.setVisibility(View.GONE);
} else {
modifyButton.setVisibility(View.GONE);
deleteButton.setVisibility(View.GONE);
}
Log.i(LOG_TAG, "ArticleActivity Created");
}
@Override
public void onClick(View v) {
if(v.equals(addButton)) {
String title = titleEdit.getText().toString();
String abs = abstractEdit.getText().toString();
String url = urlEdit.getText().toString();
Intent result = new Intent();
result.putExtra(Articles.TITLE, title);
result.putExtra(Articles.ABSTRACT, abs);
result.putExtra(Articles.URL, url);
setResult(Activity.RESULT_OK, result);
finish();
} else if(v.equals(modifyButton)){
String title = titleEdit.getText().toString();
String abs = abstractEdit.getText().toString();
String url = urlEdit.getText().toString();
Intent result = new Intent();
result.putExtra(Articles.ID, articleId);
result.putExtra(Articles.TITLE, title);
result.putExtra(Articles.ABSTRACT, abs);
result.putExtra(Articles.URL, url);
result.putExtra(EDIT_ARTICLE_ACTION, MODIFY_ARTICLE);
setResult(Activity.RESULT_OK, result);
finish();
} else if(v.equals(deleteButton)) {
Intent result = new Intent();
result.putExtra(Articles.ID, articleId);
result.putExtra(EDIT_ARTICLE_ACTION, DELETE_ARTICLE);
setResult(Activity.RESULT_OK, result);
finish();
} else if(v.equals(cancelButton)) {
setResult(Activity.RESULT_CANCELED, null);
finish();
}
}
}
package shy.luo.article; import shy.luo.providers.articles.Articles; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class ArticleActivity extends Activity implements View.OnClickListener { private final static String LOG_TAG = "shy.luo.article.ArticleActivity"; public final static String EDIT_ARTICLE_ACTION = "EDIT_ARTICLE_ACTION"; public final static int MODIFY_ARTICLE = 1; public final static int DELETE_ARTICLE = 2; private int articleId = -1; private EditText titleEdit = null; private EditText abstractEdit = null; private EditText urlEdit = null; private Button addButton = null; private Button modifyButton = null; private Button deleteButton = null; private Button cancelButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.article); titleEdit = (EditText)findViewById(R.id.edit_article_title); abstractEdit = (EditText)findViewById(R.id.edit_article_abstract); urlEdit = (EditText)findViewById(R.id.edit_article_url); addButton = (Button)findViewById(R.id.button_add_article); addButton.setOnClickListener(this); modifyButton = (Button)findViewById(R.id.button_modify); modifyButton.setOnClickListener(this); deleteButton = (Button)findViewById(R.id.button_delete); deleteButton.setOnClickListener(this); cancelButton = (Button)findViewById(R.id.button_cancel); cancelButton.setOnClickListener(this); Intent intent = getIntent(); articleId = intent.getIntExtra(Articles.ID, -1); if(articleId != -1) { String title = intent.getStringExtra(Articles.TITLE); titleEdit.setText(title); String abs = intent.getStringExtra(Articles.ABSTRACT); abstractEdit.setText(abs); String url = intent.getStringExtra(Articles.URL); urlEdit.setText(url); addButton.setVisibility(View.GONE); } else { modifyButton.setVisibility(View.GONE); deleteButton.setVisibility(View.GONE); } Log.i(LOG_TAG, "ArticleActivity Created"); } @Override public void onClick(View v) { if(v.equals(addButton)) { String title = titleEdit.getText().toString(); String abs = abstractEdit.getText().toString(); String url = urlEdit.getText().toString(); Intent result = new Intent(); result.putExtra(Articles.TITLE, title); result.putExtra(Articles.ABSTRACT, abs); result.putExtra(Articles.URL, url); setResult(Activity.RESULT_OK, result); finish(); } else if(v.equals(modifyButton)){ String title = titleEdit.getText().toString(); String abs = abstractEdit.getText().toString(); String url = urlEdit.getText().toString(); Intent result = new Intent(); result.putExtra(Articles.ID, articleId); result.putExtra(Articles.TITLE, title); result.putExtra(Articles.ABSTRACT, abs); result.putExtra(Articles.URL, url); result.putExtra(EDIT_ARTICLE_ACTION, MODIFY_ARTICLE); setResult(Activity.RESULT_OK, result); finish(); } else if(v.equals(deleteButton)) { Intent result = new Intent(); result.putExtra(Articles.ID, articleId); result.putExtra(EDIT_ARTICLE_ACTION, DELETE_ARTICLE); setResult(Activity.RESULT_OK, result); finish(); } else if(v.equals(cancelButton)) { setResult(Activity.RESULT_CANCELED, null); finish(); } } }��ArticleActivity�����У����ǿ���ִ�����������º�ɾ��������Ϣ�IJ������������ArticleActivityʱ��û�а�����ID����������˵��Ҫִ�в���������������Ϣ���������ArticleActivityʱ��������ID���������Զ��������ˣ���˵��Ҫִ�еIJ����Ǹ��»���ɾ�����£������û��ڽ��������Ǹ��°�ť����ɾ����ť��ȷ����
����ʹ�õ��Ľ����ļ�������res/layoutĿ¼�£����У�main.xml�ļ�����MainActivity�Ľ��棬��������������ʾ��
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<ListView
android:id="@+id/listview_article"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/border"
android:choiceMode="singleChoice">
</ListView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginTop="10dp">
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="@string/add">
</Button>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom"> <ListView android:id="@+id/listview_article" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/border" android:choiceMode="singleChoice"> </ListView> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center" android:layout_marginTop="10dp"> <Button android:id="@+id/button_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="15dp" android:paddingRight="15dp" android:text="@string/add"> </Button> </LinearLayout> </LinearLayout>item.xml�ļ�������ListView��ÿһ��������Ϣ��Ŀ����ʾ���棬��������������ʾ��
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textview_article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/textview_article_abstract"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/textview_article_url"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
</TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textview_article_title" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/textview_article_abstract" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/textview_article_url" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp"> </TextView> </LinearLayout>article.xml�ļ�������ArticleActivity�Ľ��棬��������������ʾ��
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="24dp"
android:text="@string/title">
</TextView>
<EditText
android:id="@+id/edit_article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/abs">
</TextView>
<EditText
android:id="@+id/edit_article_abstract"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="27dp"
android:text="@string/url">
</TextView>
<EditText
android:id="@+id/edit_article_url"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_marginTop="10dp">
<Button
android:id="@+id/button_modify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/modify">
</Button>
<Button
android:id="@+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete">
</Button>
<Button
android:id="@+id/button_add_article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="@string/add">
</Button>
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel">
</Button>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="24dp" android:text="@string/title"> </TextView> <EditText android:id="@+id/edit_article_title" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/abs"> </TextView> <EditText android:id="@+id/edit_article_abstract" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="27dp" android:text="@string/url"> </TextView> <EditText android:id="@+id/edit_article_url" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center" android:layout_marginTop="10dp"> <Button android:id="@+id/button_modify" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/modify"> </Button> <Button android:id="@+id/button_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/delete"> </Button> <Button android:id="@+id/button_add_article" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="16dp" android:paddingRight="16dp" android:text="@string/add"> </Button> <Button android:id="@+id/button_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/cancel"> </Button> </LinearLayout> </LinearLayout>��res/drawableĿ¼�£���һ��border.xml�ļ�������MainActivity�����ϵ�ListView�ı�������������������ʾ��
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000ff"/>
<stroke android:width="1dp"
android:color="#000000">
</stroke>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp">
</padding>
<corners android:radius="10dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ff0000ff"/> <stroke android:width="1dp" android:color="#000000"> </stroke> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp"> </padding> <corners android:radius="10dp" /> </shape>����ʹ�õ����ַ�����Դ�ļ�������res/values/strings.xml�ļ��У���������������ʾ��
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Article</string>
<string name="article">Article</string>
<string name="add">Add</string>
<string name="modify">Modify</string>
<string name="delete">Delete</string>
<string name="title">Title:</string>
<string name="abs">Abstract:</string>
<string name="url">URL:</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
</resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Article</string> <string name="article">Article</string> <string name="add">Add</string> <string name="modify">Modify</string> <string name="delete">Delete</string> <string name="title">Title:</string> <string name="abs">Abstract:</string> <string name="url">URL:</string> <string name="ok">OK</string> <string name="cancel">Cancel</string> </resources>����������������������ļ�AndroidManifest.xml��
[html] view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="shy.luo.article"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<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>
<activity
android:name=".ArticleActivity"
android:label="@string/article">
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="shy.luo.article" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <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> <activity android:name=".ArticleActivity" android:label="@string/article"> </activity> </application> </manifest>����ű�Android.mk������������ʾ��
[html] view plaincopyprint?
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_JAVA_LIBRARIES := libArticlesProvider
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Article
include $(BUILD_PACKAGE)
###################################################
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libArticlesProvider:./libs/ArticlesProvider.jar
include $(BUILD_MULTI_PREBUILT)
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_STATIC_JAVA_LIBRARIES := libArticlesProvider LOCAL_SRC_FILES := $(call all-subdir-java-files) LOCAL_PACKAGE_NAME := Article include $(BUILD_PACKAGE) ################################################### include $(CLEAR_VARS) LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := libArticlesProvider:./libs/ArticlesProvider.jar include $(BUILD_MULTI_PREBUILT)�������ű��������������ֵ�ָ�һ���ǰ�libsĿ¼�µ�Ԥ���뾲̬��ArticlesProvider.jar�����һ���ؾ�̬��libArticlesProvider��������ؿ��ļ�������out/target/common/obj/JAVA_LIBRARIES/libArticlesProvider_intermediatesĿ¼�£���һ�����DZ������ǵij���Article�ˣ���ͨ��LOCAL_STATIC_JAVA_LIBRARIES����������ǰ���libArticlesProvider�⣬��������������������������ArticlesProvider���Content Provider�е����ݵij�����
�������ǾͿ��Ա���ʹ�����Ӧ�ó����ˣ�
[html] view plaincopyprint?
USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Article
USER-NAME@MACHINE-NAME:~/Android$ make snod
USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Article USER-NAME@MACHINE-NAME:~/Android$ make snod
����������õ�Androidϵͳ�����ļ�system.img�Ͱ�������������������ArticleӦ�ó����ˡ�
���������ģ�������������ǵ������ˡ����������AndroidԴ���빤��������ģ��������ο���Ubuntu�����ء�����Ͱ�װAndroid����Դ����һ�ġ�
ִ��������������ģ������
[html] view plaincopyprint?
USER-NAME@MACHINE-NAME:~/Android$ emulator
USER-NAME@MACHINE-NAME:~/Android$ emulator
���Ӧ�ó��������������ͼ��ʾ��
��������Add��ť�����������µ�������Ϣ��Ŀ��
��ǰһ��������ļ��б��У����ijһ��������Ŀ������Ը��»���ɾ��������Ϣ��Ŀ��
������Content Provider��ʹ��ʵ���ͽ������ˡ���ƪ���µ�Ŀ����ʹ���߶�Content Provider��һ����ŵ��˽���Ե���ʶ������һƪ�����У����ǽ���ϸ����ArticleӦ�ó�������λ��ArticlesProvider���ContentProvider�ӿڵģ�ֻ�л��������ӿ�֮��ArticleӦ�ó�����ܷ���ArticlesProvider�����ݣ������ע��
����ת�ԣ�http://blog.csdn.net/luoshengyang/article/details/6950440
- �������
- AndroidӦ�ó������Content Provider��Ҫ���ܺ�ѧϰ�ƻ� (31�����)
- Android����������ndk-stack���ndk�����ж�̬��.so���������� (2360�����)
- Android��������θı�RadioButton����ͼƬ�����ֵ����λ�� (3391�����)
- android��������֮ADT��װ��ж�أ����� (98�����)
- Android����֮ListViewҳüҳ��Ч��VS android�������� (94�����)
- 1¥ ������ ������ 2015-3-14 15:04:07
- ���������£����������ױ�. ������ http://www.zhongguohelanwang.com/