????2.???????????ContentProvider???
????1).Family.java????е??????????????????????????????Uri?????????£?
package com.steven.hu.cpt;
import android.net.Uri;
import android.provider.BaseColumns;
public class Family
{
//????Authority????AndroidManifest.xml????ж???????
public static final String AUTHRITY = "com.steven.hu.provider.test";
public static final class Member implements BaseColumns
{
//???????????????
public static final String _ID = "_id";
public static final String NAME = "name";
public static final String AGE = "age";
public static final String GENDER = "gender";
//???????????uri
public static final Uri FAMILY_CONTENT_URI =
Uri.parse("content://" + AUTHRITY + "/family");
//??????????????uri
public static final Uri MEMBER_CONTENT_URI =
Uri.parse("content://" + AUTHRITY + "/member/");
//????
public static final String TABLE_NAME = "genealogy";
}
}
????2).ProviderToBeTest.java?е???????????????ContentProvider????????????£?
package com.steven.hu.cpt;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;
public class ProviderToBeTest extends ContentProvider
{
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "family_info.db";
private static final String TAG = "ProviderToBeTest";
private DatabaseHelper mOpenHelper;
private static UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int FAMILY = 1;
private static final int MEMBER = 2;
static
{
sMatcher.addURI(Family.AUTHRITY?? "family"?? FAMILY);
sMatcher.addURI(Family.AUTHRITY?? "member/#"?? MEMBER);
}
@Override
public boolean onCreate()
{
// TODO Auto-generated method stub
mOpenHelper = new DatabaseHelper(getContext());
return true;
}
// ????????
@Override
public Uri insert(Uri uri?? ContentValues values)
{
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
switch (sMatcher.match(uri))
{
// ???Uri????????????????????
case FAMILY:
// ????????????????????ID
long rowId = db.insert(Family.Member.TABLE_NAME?? Family.Member._ID?? values);
// ?????????????uri
if (rowId > 0)
{
// ?????е? Uri????????ID
Uri memberUri = ContentUris.withAppendedId(uri?? rowId);
// ????????????
getContext().getContentResolver().notifyChange(memberUri?? null);
return memberUri;
}
break;
default:
throw new IllegalArgumentException("δ?Uri:" + uri);
}
return null;
}
// ???????
@Override
public int delete(Uri uri?? String where?? String[] whereArgs)
{
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
// ??????????????
int num = 0;
// ????uri???????
switch (sMatcher.match(uri))
{
// ???Uri????????????????????
case FAMILY:
num = db.delete(Family.Member.TABLE_NAME?? where?? whereArgs);
break;
// ???Uri????????????????????
case MEMBER:
// ??????????????????ID
long id = ContentUris.parseId(uri);
String whereClause = Family.Member._ID + "=" + id;
// ????????where??????????where???
if (where != null && !where.equals(""))
{
whereClause = whereClause + " and " + where;
}
num = db.delete(Family.Member.TABLE_NAME?? whereClause?? whereArgs);
break;
default:
throw new IllegalArgumentException("δ?Uri:" + uri);
}
// ????????????
getContext().getContentResolver().notifyChange(uri?? null);
return num;
}
// ???????
@Override
public Cursor query(Uri uri?? String[] projection?? String where?? String[] whereArgs??
String sortOrder)
{
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
switch (sMatcher.match(uri))
{
// ???Uri????????????????????
case FAMILY:
// ??в??
return db.query(Family.Member.TABLE_NAME?? projection?? where?? whereArgs?? null?? null?? sortOrder);
// ???Uri????????????????????
case MEMBER:
// ??????????????ID
long id = ContentUris.parseId(uri);
String whereClause = Family.Member._ID + "=" + id;
// ????????where??????????where???
if (where != null && !"".equals(where))
{
whereClause = whereClause + " and " + where;
}
return db.query(Family.Member.TABLE_NAME?? projection?? whereClause?? whereArgs?? null?? null??
sortOrder);
default:
throw new IllegalArgumentException("δ?Uri:" + uri);
}
}
// ???uri?е?MIME????
@Override
public String getType(Uri uri)
{
// TODO Auto-generated method stub
switch (sMatcher.match(uri))
{
case FAMILY:
return "famliy/com.steven.hu.genealogy";
case MEMBER:
return "member/com.steven.hu.genealogy";
default:
throw new IllegalArgumentException("δ?Uri:" + uri);
}
}
// ????????
@Override
public int update(Uri uri?? ContentValues values?? String selection?? String[] selectionArgs)
{
// TODO Auto-generated method stub
return 0;
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
// ???崴?????SQL???
//final String CREATE_TABLE_SQL = "create table family(_id integer primary key autoincrement ?? name ?? age?? gender)";
final String CREATE_TABLE_SQL = "CREATE TABLE genealogy"  + " ("
+ Family.Member._ID + " INTEGER PRIMARY KEY??"
+ Family.Member.NAME + " TEXT??"
+ Family.Member.AGE + " INTEGER??"
+ Family.Member.GENDER + " TEXT"
+ ");";
public DatabaseHelper(Context context)
{
super(context?? DATABASE_NAME?? null?? DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
// ????
db.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db?? int oldVersion?? int newVersion)
{
// TODO Auto-generated method stub
Log.w(TAG?? "Upgrading database from version " + oldVersion + " to "
+ newVersion + "?? which will destroy all old data");
// Kills the table and existing data
db.execSQL("DROP TABLE IF EXISTS genealogy");
// Recreates the database with a new version
onCreate(db);
}
}
public DatabaseHelper getOpenHelperForTest()
{
return mOpenHelper;
}
}