Implementation of Android content providers and provision of android content

Source: Internet
Author: User

Implementation of Android content providers and provision of android content

Next, we will further implement content providers in the introduction to Android content providers.

Each Content Provider class uses URI (Universal Resource Identifier) as its independent Identifier, in the format of content: // com. example. app. provider/table1. Other applications access different content providers through different Uris and obtain/manipulate the data.

For example, this project corresponds to the following URI:

Content: // com. wuyudong. db. personprovider/insert
Content: // com. wuyudong. db. personprovider/delete operation
Content: // com. wuyudong. db. personprovider/update
Content: // com. wuyudong. db. personprovider/query

Add code in PersonDBProvider. java:

Package com. wuyudong. db; import android. content. contentProvider; import android. content. contentValues; import android. content. uriMatcher; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android.net. uri; public class PersonDBProvider extends ContentProvider {// defines a URI's matcher to match the uri. If the path does not meet the conditions, the system returns-1 private static UriMatcher matcher = new UriMatcher (UriMatcher. NO_MATCH); private static final int INSERT = 1; private static final int DELETE = 2; private static final int UPDATE = 3; private static final int QUERY = 4; private PersonSQLiteOpenHelper helper; static {// Add a group of matching rules com. wuyudong. db. personprovider matcher. addURI ("com. wuyudong. db. personprovider "," insert ", INSERT); matcher. addURI ("com. wuyudong. db. personprovider "," delete ", DELETE); matcher. addURI ("com. wuyudong. db. personprovider "," update ", UPDATE); matcher. addURI ("com. wuyudong. db. personprovider "," query ", QUERY) ;}@ Override public boolean onCreate () {helper = new PersonSQLiteOpenHelper (getContext (); return false ;} @ Override public Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) {if (matcher. match (uri) = QUERY) {// return the QUERY result set SQLiteDatabase db = helper. getReadableDatabase (); Cursor cursor = db. query ("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor;} else {throw new IllegalArgumentException ("path mismatch, you cannot perform the query operation ");} @ Override public String getType (Uri uri) {// TODO Auto-generated method stub return null;} @ Override public Uri insert (Uri uri Uri, contentValues values) {// TODO Auto-generated method stub return null;} @ Override public int delete (Uri uri, String selection, String [] selectionArgs) {// TODO Auto-generated method stub return 0;} @ Override public int update (Uri uri, ContentValues values, String selection, String [] selectionArgs) {// TODO Auto-generated method stub return 0 ;}}

Create a project named other with the following layout:

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "> <Button android: onClick =" click "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "read database data"/> </RelativeLayout>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "> <Button android: onClick =" click "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "read DB data"/> <Button android: onClick = "delete" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "delete DB data"/> <Button android: onClick = "update" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Modify DB data"/> <Button android: onClick = "insert" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "add DB data"/> </LinearLayout>

Next we will implement other methods in the PersonDBProvider.

Package com. wuyudong. db; import android. content. contentProvider; import android. content. contentValues; import android. content. uriMatcher; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android.net. uri; public class PersonDBProvider extends ContentProvider {// defines a URI's matcher to match the uri. If the path does not meet the conditions, the system returns-1 private static UriMatcher matcher = new UriMatcher (UriMatcher. NO_MATCH); private static final int INSERT = 1; private static final int DELETE = 2; private static final int UPDATE = 3; private static final int QUERY = 4; private PersonSQLiteOpenHelper helper; static {// Add a group of matching rules com. wuyudong. db. personprovider matcher. addURI ("com. wuyudong. db. personprovider "," insert ", INSERT); matcher. addURI ("com. wuyudong. db. personprovider "," delete ", DELETE); matcher. addURI ("com. wuyudong. db. personprovider "," update ", UPDATE); matcher. addURI ("com. wuyudong. db. personprovider "," query ", QUERY) ;}@ Override public boolean onCreate () {helper = new PersonSQLiteOpenHelper (getContext (); return false ;} @ Override public Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) {if (matcher. match (uri) = QUERY) {// return the QUERY result set SQLiteDatabase db = helper. getReadableDatabase (); Cursor cursor = db. query ("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor;} else {throw new IllegalArgumentException ("path mismatch, you cannot perform the query operation ");} @ Override public String getType (Uri uri) {// TODO Auto-generated method stub return null;} @ Override public Uri insert (Uri uri Uri, contentValues values) {if (matcher. match (uri) = INSERT) {// return the query result set SQLiteDatabase db = helper. getWritableDatabase (); db. insert ("person", null, values);} else {throw new IllegalArgumentException ("path mismatch, cannot execute insert operation");} return null ;} @ Override public int delete (Uri uri, String selection, String [] selectionArgs) {if (matcher. match (uri) = DELETE) {// return the query result set SQLiteDatabase db = helper. getWritableDatabase (); db. delete ("person", selection, selectionArgs);} else {throw new IllegalArgumentException ("path mismatch, cannot perform the delete operation");} return 0 ;} @ Override public int update (Uri uri, ContentValues values, String selection, String [] selectionArgs) {if (matcher. match (uri) = UPDATE) {// return the query result set SQLiteDatabase db = helper. getWritableDatabase (); db. update ("person", values, selection, selectionArgs);} else {throw new IllegalArgumentException ("path mismatch, modification operation not allowed");} return 0 ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.