You are on page 1of 5

2.

To v s dng 1 Content Provider do ngi dng t nh ngha


d hiu hn cc bn m Project ContentProviderDemo trong Sourcecode
down v. Trong Project mnh to 1 Content Provider Books, mi bn ghi Book
bao gm 2 trng : ID v Title.
Sau y l cc bc to 1 Content Provider c bn ( c th l to
ContentProvider Book)
1. To 1 class tha k lp ContentProvider
M:
public class BookProvider extends ContentProvider
2. nh ngha 1 bin Uri (public static final ) c gi CONTENT_URI. Cc xu
ny lun c bt u bng content:// tip theo l ni dung ca m
ContentProvider x l. Xu ny phi c c tnh l duy nht.
M:
public static final String PROVIDER_NAME =
"com.vietandroid.provider.Books";
public static final Uri CONTENT_URI =
Uri.parse("content://" + PROVIDER_NAME + "/books");
3. Khai bo cc xu nh ngha cho tng thuc tnh tng ng vi cc ct gi tr
t Cursor.
M:
public static final String _ID = "_id";
public static final String TITLE = "title";
4. Chng ta cn to h thng cha d liu cho ContentProvider, c th cha di
nhiu hnh thc : s dng XML, thng qua CSDL SQLite, hay thm ch l
WebService. Trong Demo ny chng ta s dng cch ph bin nht l SQLite:

M:
private
private
"Books";
private
"titles";
private

SQLiteDatabase bookDB;
static final String DATABASE_NAME =
static final String DATABASE_TABLE =
static final int DATABASE_VERSION = 1;

5. nh ngha tn ca cc ct m chng ta s tr li gi tr cho cc clients.Nu


chng ta ang s dng Database ContentProvider hay cc lp SQLiteOpenHelper,
tn cc ct ny chnh l id ca cc ct trong c s d liu SQL. Trong trng hp
ny, chng ta phi gp c ct c gi tr l s nguyn c gi _id nh ngha
id ca mi bn ghi.
Nu ang s dng c s d liu SQLite, n s l INTEGER PRIMARY KEY
AUTOINCREMENT. Ty chn AUTOINCREMENT khng bt buc, c tc dng
t ng tng ID ca mi bn ghi ln nu ngi dng khng nhp. Android cung
cp SQLiteOpenHelper gip to v qun l cc phin bn ca c s d liu.
M:
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE +
" (_id integer primary key autoincrement, "
+ "title text not null);";
private static class DatabaseHelper extends
SQLiteOpenHelper
{
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME , null,
DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);

}
@Override
public void onUpgrade(SQLiteDatabase db, int
oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS
titles");
onCreate(db);
}
}
6. Nu chng ta mun public cc d liu kiu byte nh bitmap th cc trng m
cha d liu ny nn l mt xu vi 1 content://URI cho file . y chnh l lin
kt cc ng dng khc c th truy cp v s dng d liu bitmap ny.
7. S dng Cursor thao tc trn tp d liu : query (), update(), insert(),
delete().. C th gi phng thc ContentResolver.notifyChange() bitkhi
no d liu c cp nht.
Add Book
M:
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = bookDB.insert(DATABASE_TABLE,
"", values);
if(rowID > 0)
{
Uri mUri =
ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(mUr
i, null);
return mUri;
}
throw new SQLException("Failed to insert new
row into " + uri);
}

Get All Books


M:
@Override
public Cursor query(Uri uri, String[] projection,
String selection,
String[] selectionArgs, String
sortOrder) {
SQLiteQueryBuilder sqlBuilder = new
SQLiteQueryBuilder();
sqlBuilder.setTables(DATABASE_TABLE);
if(uriMatcher.match(uri) == BOOK_ID)
sqlBuilder.appendWhere(_ID + "=" +
uri.getPathSegments().get(1));
if(sortOrder == null || sortOrder == "")
sortOrder = TITLE;
Cursor c = sqlBuilder.query(bookDB,
projection, selection, selectionArgs, null, null,
sortOrder);
c.setNotificationUri(getContext().getContentResolv
er(), uri);
return c;
}
}
Mnh ch demo 2 chc nng l thm sch v ly ton b bn ghi trong CSDL ,
ngoi ra cc phng thc edit, sa , update, xa... cc bn c th t lm .

8. Khai bo Content Provider trong file AndroidManifest.xml


M:
<provider android:name = "BookProvider"
android:authorities="com.vietandroid.provider.Book
s" />
Nh vy chng ta to xong ContentProvider Book t nh ngha.

You might also like