giá trị mà người dùng nhập vào
@Override
public Uri insert(Uri uri, ContentValues values) {
//chen du lieu vao table cua database
long rowID = gdb.insert(TABLE_NAME, "", values);
//neu chen thanh cong thi thuc hien
if(rowID > 0)
{
//tao mot dinh dang cho dong du lieu ban moi vua them vao
Uri mUri = ContentUris.withAppendedId(CONTENT_URI, rowID);
//khoi dong bang ContentResolver de doi tuong mUri tao thanh cong
getContext().getContentResolver().notifyChange(mUri, null);
return mUri;
}
throw new SQLException("Failed to insert new row into " + uri);
}
• Cuối cùng là hàm query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) (). Hàm này:
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
//doi tuong dung de luu cac gia tri cua cau lenh truy van, gia cu moi khong de len gia tri cu
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
//set doi tuong SQLiteQueryBuilder len bang can lay du lieu
sqlBuilder.setTables(TABLE_NAME);
//su dung chuoi uriMatcher ma minh da tao, neu chuoi nay dan den vung du lieu cua table
//thi lay du lieu ra khoi table
if(uriMatcher.match(uri) == USER_ID)
sqlBuilder.appendWhere(sID + "=" + uri.getPathSegments().get(1));
//sap xep du lieu tang dan theo id
if(sortOrder == null || sortOrder == "")
sortOrder = sID;
//tao cursor bang du lieu ma minh da lay ra
Cursor cur = sqlBuilder.query(gdb, projection, selection, selectionArgs, null, null, sortOrder);
//khoi dong ContentResolver de doi tuong uri duoc hoat dong va du lieu duoc du ra
cur.setNotificationUri(getContext().getContentResolver(), uri);
return cur;
}
Tiếp đó chúng ta cần viết một số hàm trong lớp MainActivity.
• Hàm thêm dữ liệu vào database bằng cách nhận dữ liệu từ giao diện GUI:
//them mot du lieu moi vao table
private void onAdd()
{
//lay ten do nguoi dung nhap vao EditText
String name = edtName.getEditableText().toString();
//day gia tri ten vao ContentValues
ContentValues values = new ContentValues(); values.put(UserInfo.sName, name);
//dua gia tri ContentValues vao co so du lieu thong qua duong dan
Uri uriInsert = getContentResolver().insert(UserInfo.CONTENT_URI, values); //hien thong bao neu them du lieu thanh cong
if(uriInsert != null)
{
Toast.makeText(this, "User's added", Toast.LENGTH_SHORT).show(); }
}
• Hàm lấy toàn bộ dữ liệu trong cơ sở dữ liệu và hiển thị lên giao diện:
//ham hien thi toan bo du lieu
@SuppressWarnings("deprecation")
private void onDisplay()
{
StringBuilder sb = new StringBuilder(); Uri uri = UserInfo.CONTENT_URI;
//khoi tao cursor voi duong dan uri va sap xep tang dan theo id
Cursor c = managedQuery(uri, null, null, null, "ID desc"); if(c.moveToFirst()){
do{
String userRecord = "ID = " + c.getString(c.getColumnIndex(UserInfo.sID)) + " Name = " + c.getString(c.getColumnIndex(UserInfo.sName)); sb.append(userRecord); sb.append("\n"); }while(c.moveToNext()); } else { sb.append("Chua co du lieu"); } ((TextView)this.findViewById(R.id.txtDisplay)).setText(sb.toString()); }
Chúng ta cần thêm một số thông tin vào trong hàm AndroidMainfest.xml:
android:authorities="myandroid.net.User" />
trong thẻ application. Tất cả source code và demo nằm trong thư mục DemoSeminar/Seminar_CreateContentProvider
2.4.4 Sử dụng Content Provider mới tạo:
Chúng ta đã tạo thành công một content Provider. Bây giờ chúng ta sẽ sử dụng Content Provider đã tạo bằng cách sử dụng uri. Ứng dung này có một button dùng để hiển thị tồn bộ dữ liệu có trong cơ sở dư liệu của ứng dụng mà ta đã tạo ở chương trên.
Code:
private void onDisplay()
{
//dung de chua toan bo du lieu lay ra tu co so du lieu cua ung dung lien ket
StringBuilder sb = new StringBuilder();
//chuoi uri cua content provider chia se du lieu
Uri uriGetListTitles = Uri.parse("content://myandroid.net.User/users"); //cursor dung de tro den du lieu con co so
du lieu thong qua doi tuong uri
Cursor c = managedQuery (uriGetListTitles, null , null , null , "ID desc" ) ; if(c != null)
{
if(c.moveToFirst()){
do{
String bookRecord = "id = " +
c.getString(c.getColumnIndex("ID")) + " - Full Name= " +c.getString(c.getColumnIndex("Name")); sb.append(bookRecord); sb.append("\n"); }while(c.moveToNext()); } } else { sb.append("Khong co du lieu"); } ((TextView)this.findViewById(R.id.txtDisplay)).setText(sb.toString()); }