Android 內容提供者的實現

來源:互聯網
上載者:User

標籤:

接著上文《Android 內容提供者簡介》進一步實現內容提供者。

每個Content Provider類都使用URI(Universal Resource Identifier,通用資源標識符)作為獨立的標識,格式如:content://com.example.app.provider/table1。其他應用程式通過不同的uri訪問不同的內容提供者,並擷取/操作裡面的資料。

例如在本項目中對應如下URI:

content://com.wuyudong.db.personprovider/insert 添加的操作
content://com.wuyudong.db.personprovider/delete 刪除的操作
content://com.wuyudong.db.personprovider/update 更新的操作
content://com.wuyudong.db.personprovider/query 查詢的操作

在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 {    // 定義一個URI的匹配器用於匹配uri, 如果路徑不滿足條件 返回-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 {        // 添加一組匹配規則 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) {            // 返回查詢的結果集            SQLiteDatabase db = helper.getReadableDatabase();            Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);            return cursor;        } else {            throw new IllegalArgumentException("路徑不匹配,不能執行查詢操作");        }    }    @Override    public String getType(Uri uri) {        // TODO Auto-generated method stub        return null;    }    @Override    public Uri insert(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;    }}

建立一個名為other的項目,布局如下:

<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="讀取DB的資料" /></RelativeLayout>

點擊的按鈕,kill掉 com.wuyudong.db進程

點擊Button按鈕後,com.wuyudong.db進程重新運行,而且列印資料庫中person表中的資料

進一步完善其他動作,修改布局

<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="讀取DB的資料" />     <Button        android:onClick="delete"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="刪除DB的資料" />      <Button        android:onClick="update"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="修改DB的資料" />       <Button        android:onClick="insert"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="添加DB的資料" /></LinearLayout>

接下來實現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 {    // 定義一個URI的匹配器用於匹配uri, 如果路徑不滿足條件 返回-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 {        // 添加一組匹配規則 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) {            // 返回查詢的結果集            SQLiteDatabase db = helper.getReadableDatabase();            Cursor cursor = db.query("person", projection, selection,                    selectionArgs, null, null, sortOrder);            return cursor;        } else {            throw new IllegalArgumentException("路徑不匹配,不能執行查詢操作");        }    }    @Override    public String getType(Uri uri) {        // TODO Auto-generated method stub        return null;    }    @Override    public Uri insert(Uri uri, ContentValues values) {        if (matcher.match(uri) == INSERT) {            // 返回查詢的結果集            SQLiteDatabase db = helper.getWritableDatabase();            db.insert("person", null, values);        } else {            throw new IllegalArgumentException("路徑不匹配,不能執行插入操作");        }        return null;    }    @Override    public int delete(Uri uri, String selection, String[] selectionArgs) {        if (matcher.match(uri) == DELETE) {            // 返回查詢的結果集            SQLiteDatabase db = helper.getWritableDatabase();            db.delete("person", selection, selectionArgs);        } else {            throw new IllegalArgumentException("路徑不匹配,不能執行刪除操作");        }        return 0;    }    @Override    public int update(Uri uri, ContentValues values, String selection,            String[] selectionArgs) {        if (matcher.match(uri) == UPDATE) {            // 返回查詢的結果集            SQLiteDatabase db = helper.getWritableDatabase();            db.update("person", values, selection, selectionArgs);        } else {            throw new IllegalArgumentException("路徑不匹配,不能執行修改操作");        }        return 0;    }}

 

Android 內容提供者的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.