1. 程式人生 > >ContentProvider的用法與理解

ContentProvider的用法與理解

ContentProvider是用來分享資料的一個標準介面,所以在使用上它分為3部分

中間部分就是一個註冊,在manifest中加provider,由主機名和路徑組陳,表示全球唯一的一個繼承了ContentProvider並實現了抽象方法的類

ContentProvider部分就是向外部提供資料的部分,它提供了一些標準的方法讓外部操作應用程式的資料

需要使用別的應用程式的使用者,getContentResolver().query(Uri.parse("content://com.example.huanghanqing.testsqlit.usercp")),通過後面的那個引數可以拿到ContentProvider

package com.example.huanghanqing.testsqlite;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;

/**
 * Created by huanghanqing on 2016/2/17.
* 該類的作用為提供統一的介面,為外部訪問該應用的資料提供介面 */ public class UsersCP extends ContentProvider { private UsersDb db; private SQLiteDatabase dbReader,dbWriter; public static final String TABLE_NAME = "user"; //引數String型別必須與manifest中的<provider>中的authorities相同,用於對外提供公開該ContentProvider的唯一識別符號 public static final
Uri URI = Uri.parse("content://com.example.huanghanqing.testsqlit.usercp"); @Override public boolean onCreate() { db = new UsersDb(getContext()); dbReader = db.getReadableDatabase(); dbWriter = db.getWritableDatabase(); return true; } @Nullable @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { return dbReader.query(TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder); } @Nullable @Override public String getType(Uri uri) { return null; } @Nullable @Override public Uri insert(Uri uri, ContentValues values) { dbWriter.insert(TABLE_NAME,null,values); return uri; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return dbWriter.delete(TABLE_NAME,selection,selectionArgs); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return dbWriter.update(TABLE_NAME,values,selection,selectionArgs); } }
android:authorities="com.example.huanghanqing.testsqlite..usercp"
android:name="UsersCP"/>
package com.example.huanghanqing.testsqlite;


import android.support.v4.widget.SimpleCursorAdapter;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends ListActivity {

    private UsersDb db;
    EditText nameEditText;
    EditText sexEditText;
    Button add;
    SQLiteDatabase dbWriter;
    SQLiteDatabase dbReader;
    SimpleCursorAdapter adapter;
    Cursor c;
    ListView listView;

    @SuppressWarnings("deprecation")
    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new UsersDb(this);
        dbReader = db.getReadableDatabase();
        dbWriter = db.getWritableDatabase();

        nameEditText = (EditText) findViewById(R.id.editTextName);
        sexEditText = (EditText) findViewById(R.id.editTextSex);
        add = (Button) findViewById(R.id.add);
        //listView = (ListView)findViewById(R.id.listView1);
c = dbReader.query("user", null, null, null, null, null, null);
        //c = getContentResolver().query(UsersCP.URI,)
adapter = new SimpleCursorAdapter(this, R.layout.test_sqlite_item, c, new String[]{"name", "sex"}, new int[]{R.id.name, R.id.sex});
        setListAdapter(adapter);
        add.setOnClickListener(new OnClickListener() {

            @Override
public void onClick(View v) {
                // TODO Auto-generated method stub
ContentValues cv = new ContentValues();
                cv.put("name", nameEditText.getText().toString());
                cv.put("sex", sexEditText.getText().toString());
                //dbWriter.insert("user", null, cv);
getContentResolver().insert(UsersCP.URI,cv);//拿到該uri就代表獲得了該應用的ContentProvider
refresh();
            }
        });
        getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           int position, long id) {
                // TODO Auto-generated method stub
return false;
            }
        });
    }

    public void refresh() {
//        c = dbReader.query("user", null, null, null, null, null, null);
//        adapter.changeCursor(c);
adapter.changeCursor(getContentResolver().query(UsersCP.URI,null,null,null,null));
    }
}