安卓四大基本组件之一 ContentProvider(自定义内容提供者实例)

2/10/2017来源:ASP.NET技巧人气:1998

ContentPRovider将应用中的数据对其他应用进行共享,提供增删改查的方法

首先在数据提供者的demo创建一个类,继承ContentProvider

package com.provide;

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

import db.DbHelper;

/**
 * 内容提供者
 * Created by yangyan on 2017/2/6.
 */

public class mycontentprovide extends ContentProvider {

    private DbHelper dbHelper;
    private SQLiteDatabase sqLiteDatabase;
    private UriMatcher uriMatcher;

    @Override
    public boolean onCreate() {
        Log.i("test","onCreate");
        //uri匹配器
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //查询所有规则
        uriMatcher.addURI("com.example.android16_sqllite.PERSON","person",1);
        //查询单个规则
        uriMatcher.addURI("com.example.android16_sqllite.PERSON","person/#",2);
        dbHelper = new DbHelper(getContext(),"person.db",null,2);
        sqLiteDatabase = dbHelper.getReadableDatabase();
        return false;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
        Log.i("test","查询");
        //根据匹配器匹配
        int type=uriMatcher.match(uri);
        switch (type) {
            case 1:
                //查询所有
                Log.i("test","查询所有");
                return sqLiteDatabase.query(false,"person",strings,s,strings1,null,null,s1,null);
                case 2:
                    //获取要查询的值
                    long id=ContentUris.parseId(uri);
                    Log.i("test","查询单个");
                return sqLiteDatabase.query(false,"person",strings,"_id=?",new String[]{id+""},null,null,s1,null);
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        Log.i("test","getType");
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        Log.i("test","添加");
        sqLiteDatabase.insert("person",null,contentValues);
        return uri;
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {
        Log.i("test","删除");
        return sqLiteDatabase.delete("person",s,strings);
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
        Log.i("test","修改");
        return sqLiteDatabase.update("person",contentValues,s,strings);
    }
}

!需要在androidMainfest.xml配置ContentProvider (name为类名,authorities为包名.自定义明智)


 // 需在创建个内容访问的demo访问数据的测试类
package com.example.android24_contentprovide;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 内容访问者
 */

public class MainActivity extends AppCompatActivity {

    private ContentResolver contentResolver;
    private EditText et_main_text;
    private EditText et_main_text2;
    private EditText et_main_text3;
    private EditText et_main_text4;
    private EditText et_main_text5;
    private Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取内容访问者
        contentResolver = getContentResolver();
        initview();
    }
    //初始化控件
    public void  initview(){
        et_main_text = (EditText) findViewById(R.id.et_main_text);
        et_main_text2 = (EditText) findViewById(R.id.et_main_text2);
        et_main_text3 = (EditText) findViewById(R.id.et_main_text3);
        et_main_text4 = (EditText) findViewById(R.id.et_main_text4);
        et_main_text5 = (EditText) findViewById(R.id.et_main_text5);
    }
    //添加
    public void add(View view){
 	  // 访问内容提供者  
        Uri  uri = Uri.parse("content://com.example.android16_sqllite.PERSON");
        ContentValues contentValues = new ContentValues();
        String name=et_main_text2.getText().toString();
        int age=Integer.parseInt(et_main_text3.getText().toString());
        contentValues.put("name",name);
        contentValues.put("age",age);
        contentResolver.insert(uri,contentValues);
        Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        Log.i("test","添加");
    }
    //修改
    public void update(View view){
          // 访问内容提供者  
        Uri  uri = Uri.parse("content://com.example.android16_sqllite.PERSON");
        ContentValues  contentValues = new ContentValues();
        String name=et_main_text4.getText().toString();
        int age=Integer.parseInt(et_main_text5.getText().toString());
        contentValues.put("name",name);
        contentValues.put("age",age);
        contentResolver.update(uri,contentValues,"_id=?",new String[]{5+""});
        Log.i("test","修改");
        Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
    }
    //删除
    public void delete(View view){
        // 访问内容提供者  
        Uri uri = Uri.parse("content://com.example.android16_sqllite.PERSON");
        contentResolver.delete(uri,"_id=?",new String[]{1+""});
        Log.i("test","删除");
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
    }

    //查询
    public void queryall(View view){
        //判断输入框是否为空
        Uri uri;
        if(TextUtils.isEmpty(et_main_text.getText().toString())){
        //内容协议:content://
            uri = Uri.parse("content://com.example.android16_sqllite.PERSON/person");
        }else{
            String id=et_main_text.getText().toString();
            //查询单个
            uri = Uri.parse("content://com.example.android16_sqllite.PERSON/person/"+id);
        }
        //查询所有数据
        cursor = contentResolver.query(uri,null,null,null,null);
        while (cursor.moveToNext()){
            int id= cursor.getInt(cursor.getColumnIndex("_id"));
            String name= cursor.getString(cursor.getColumnIndex("name"));
            int age= cursor.getInt(cursor.getColumnIndex("age"));
            Log.i("test","id: "+id+"name: "+name+"age: "+age);
        }
    }
}