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中的中的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));
    }
}

你可能感兴趣的:(android)