十三、ContentProvider案例

发布于:2021-07-30 13:46:25

PersonContentProvider内容提供者类





package
com.ljq.db;
* 内容提供者
*
* 作用:统一数据访问方式,方便外部调用
*
*
@author
jiqinlin
*
}
@Override
dbOpenHelper
=

new
DBOpenHelper(
this
.getContext());
}
@Override
SQLiteDatabase db
=
dbOpenHelper.getWritableDatabase();
id
=
db.insert(
"
person
"
,
"
name
"
, values);
//
返回的是记录的行号,主键为int,实际上就是主键值

id
=
db.insert(
"
person
"
,
"
name
"
, values);
String path
=
uri.toString();
}
}
@Override
SQLiteDatabase db
=
dbOpenHelper.getWritableDatabase();
count
=
db.delete(
"
person
"
, selection, selectionArgs);
String where
=

"
id=
"

+
personid;
//
删除指定id的记录

}
db.close();
}
@Override
String[] selectionArgs) {
SQLiteDatabase db
=
dbOpenHelper.getWritableDatabase();
count
=
db.update(
"
person
"
, values, selection, selectionArgs);
String where
=

"
id=
"

+
personid;
//
获取指定id的记录

}
db.close();
}
@Override
}
}
@Override
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db
=
dbOpenHelper.getReadableDatabase();
String where
=

"
id=
"

+
personid;
//
获取指定id的记录

}
}

????????????????????


文件清单





xml version="1.0" encoding="utf-8"
?>

package
="com.ljq.sql"
android:versionCode
="1"

android:versionName
="1.0"
>

android:label
="@string/app_name"
>

android:label
="@string/app_name"
>

android:authorities
="com.ljq.provider.personprovider"

/>

android:targetPackage
="com.ljq.sql"
android:label
="Tests for My App"

/>


???????????????????


PersonContentProviderTest内容提供者测试类





package
com.ljq.test;
* 外部访问内容提供者
*
*
@author
jiqinlin
*
ContentResolver contentResolver
=

this
.getContext().getContentResolver();
Uri insertUri
=
Uri.parse(
"
content://com.ljq.provider.personprovider/person
"
);
ContentValues values
=

new
ContentValues();
values.put(
"
name
"
,
"
ljq
"
);
values.put(
"
phone
"
,
"
1350000009
"
);
Uri uri
=
contentResolver.insert(insertUri, values);
Log.i(TAG, uri.toString());
}
ContentResolver contentResolver
=

this
.getContext().getContentResolver();
Uri updateUri
=
Uri.parse(
"
content://com.ljq.provider.personprovider/person/1
"
);
ContentValues values
=

new
ContentValues();
values.put(
"
name
"
,
"
linjiqin
"
);
contentResolver.update(updateUri, values,
null
,
null
);
}
ContentResolver contentResolver
=

this
.getContext().getContentResolver();
Cursor cursor
=
contentResolver.query(uri,
null
,
null
,
null
,
"
id asc
"
);
String name
=
cursor.getString(cursor.getColumnIndex(
"
name
"
));
String phone
=
cursor.getString(cursor.getColumnIndex(
"
phone
"
));
Log.i(TAG,
"
personid=
"
+
personid
+

"
,name=
"
+
name
+

"
,phone=
"
+
phone);
}
cursor.close();
}
ContentResolver contentResolver
=

this
.getContext().getContentResolver();
Uri uri
=
Uri.parse(
"
content://com.ljq.provider.personprovider/person/1
"
);
contentResolver.delete(uri,
null
,
null
);
}
}







相关资源:ContentProvider案例

相关推荐

最新更新

猜你喜欢