ContentProvider内容提供者,自定义

先创建一个数据库吧

package com.example.sqlitedatabase_transaction;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class Mysq extends SQLiteOpenHelper{

    public Mysq(Context context) {
        super(context, "wode.db", null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table mlove(_id integer primary key autoincrement,name varchar(20),count float(11,3))");
        db.execSQL("insert into mlove values(null,'小明',1000)");
        db.execSQL("insert into mlove values(null,'小刚',0)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        
    }

}

然后我又创建了内容提供者他实现了数据库

package com.example.sqlitedatabase_transaction;

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;
import android.util.Log;

public class Mycontent extends ContentProvider{
    public final static int code=1;
    public final static int code2=2;
    private String sqlname="mlove";
    static UriMatcher uriMatcher;
    private SQLiteDatabase db;
static{
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);//路径匹配
    uriMatcher.addURI("com.wode", "A", code);//匹配的规则 参数1是在清单中定义的auth...写上 2,判断3的int值
    uriMatcher.addURI("com.wode", "B", code2);//果2参都一如样下面会替代上面
}
    @Override
    public boolean onCreate() {//用于创建已有的数据库对象
        Mysq mysq = new Mysq(getContext());
        db = mysq.getReadableDatabase();
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        int match = uriMatcher.match(uri);//匹配路径返回code的int值//如果uri和addURI不匹配返回-1
        Cursor query=null;
        switch (match) {
        case code:
            Log.e("tag", match+"code");
            query = db.query(sqlname, projection, selection, selectionArgs, null, null, sortOrder);
            break;
        case code2:
            Log.e("tag", match+"code2");
            break;
        default:
            break;
        }
        return query;
    }

    @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
        long l = db.insert(sqlname, null, values);//如果2参数是3没有值自动填写null
        Uri uri2 =null;
        if(l!=-1){
         uri2 = Uri.parse(""+l);//我这里用uri2没有引用,new出来
         Log.e("tag", uri2+""); 
        }else{
            return null;    
        }
        return uri2;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
int i = db.delete(sqlname, selection, selectionArgs);
        
        return i;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        int update = db.update(sqlname, values, selection, selectionArgs);
        return update;
    }

}

在代码中写了四个点击方法

package com.example.sqlitedatabase_transaction;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

    private ContentResolver resolver;
    private Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Mysq mysq = new Mysq(this);
        SQLiteDatabase database = mysq.getReadableDatabase();
        uri = Uri.parse("content://com.wode/A");//引用类(如果为空的话select不会崩溃因为他没有传入值!..毫无反应)
    }
    public void select(View v){
resolver = getContentResolver();
        
//      通过uri走清单com.wode然后跳转Mycontent类掉方法       A就是判断int值
        Cursor query = resolver.query(uri, null, null, null, null, null);
        if(query!=null){
            query.moveToFirst();
            query.moveToPrevious();
            while(query.moveToNext()){
                int _id = query.getInt(0);
                String name = query.getString(1);
                float count = query.getInt(2);
                Log.e("tag", _id+name+count+"");
            }
        }
    }
    public void delete(View v){
        int i = resolver.delete(uri, "name=?", new String[]{"小木棍"});
        if(i>0){
            Log.e("tag", "删除成功");
        }else{
            Log.e("tag", "删除失败");
        }
    }
    public void update(View v){
        ContentValues contentValues = new ContentValues();
        contentValues.put("count", 11.55);
        int i = resolver.update(uri, contentValues, "name=?", new String[]{"小木棍"});
        if(i>0){
            Log.e("tag", "修改成功");
        }else{
            Log.e("tag", "修改失败");
        }
    }
    public void insert(View v){
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", "小木棍");
        contentValues.put("count", "823.5");
        Uri insert = resolver.insert(uri, contentValues);
        if(insert==null){
            Log.e("tag", "添加失败");
        }else{
            String path = insert.getPath();
            Log.e("tag", "添第"+path+"个数据");  
        }
            
    }
}

主布局



    

清单中注册


            android:exported属性设置成true:可被其他应用使用;
android:exported属性设置成false:只能被自己所在的应用使用;
        

你可能感兴趣的:(ContentProvider内容提供者,自定义)