Android基础知识ContentProvider

                          ContentProvider 主要用于在不同的程序之间实现数据共享

一、内容提供器的用法

一种是使用现有的内容提供器来读取和操作相应程序中的数据

一种是创建自己的内容提供器给我们的程序的数据提供外部接口

二、怎样访问内容提供器中的数据

                       借助ContentResolver类,通过Context的getContentResolver()方法获取到该类的实例

ContentResolver中提供了一系列的方法对数据进行CRUD操作,insert()插入    update()更新 delete()删除 query()查找

ContentResolver中的增删改查方法是不接受表名参数的,用一个Uri参数替代。可以调用Uri.parse()方法将内容URI字符串解析成Uri对象。

Cursor cursor=getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder );  查询完以后返回的仍然是一个Cursor对象,这个时候可以将对象中的数据数据读取出来。

query()方法中参数的含义:

uri   指定查询某一个应用程序下的某张表          projection 指定查询的列名          selection   指定where的约束条件

selectionArgs  为where中的占位符提供具体的值     orderBy 指定查询结果的排序方式

取出query()中的数据:

if(cursor!=null){

while(cursor.moveToNext()){

String xxx=cursor.getString(cursor.getColumnIndex("  "));

 

}

cursor.close();

}

三、怎样向表中添加数据

将添加的数据组装到 ContentValues中,然后在调用ContentResolver的insert()方法。

/**

ContentValues values= new ContentValues();

values.put(" ",  );

values.put(" ",)

getContentResolver().insert(uri,values);

**/

同样更新的话也是这样

四、创建自己的内容提供器

新建一个类继承自ContentProvider

package com.example.administrator.mylmada;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

public class MyProvider extends ContentProvider {
    public  static  final  int TABLE1_DIR=0;
    public  static  final  int TABLE1_ITEM=1;
    public  static  final  int TABLE2_DIR=2;
    public  static  final  int TABLE2_ITEM=3;

    static  {

        UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI("","",TABLE1_DIR);
        uriMatcher.addURI("","",TABLE1_ITEM);
        uriMatcher.addURI("","",TABLE2_DIR);
        uriMatcher.addURI("","",TABLE2_ITEM);

    }

    @Override
    public boolean onCreate() {
        return false;
    }

    @androidx.annotation.Nullable
    @Override
    public Cursor query(@androidx.annotation.NonNull Uri uri, @androidx.annotation.Nullable String[] projection, @androidx.annotation.Nullable String selection, @androidx.annotation.Nullable String[] selectionArgs, @androidx.annotation.Nullable String sortOrder) {

      //具体的逻辑
        return null;
    }

    @androidx.annotation.Nullable
    @Override
    public String getType(@androidx.annotation.NonNull Uri uri) {
     switch(uriMacher.match(uri)){
     //具体的逻辑
  
                 }
        return null;
    }

    @androidx.annotation.Nullable
    @Override
    public Uri insert(@androidx.annotation.NonNull Uri uri, @androidx.annotation.Nullable ContentValues values) {
       //具体的逻辑 
       return null;
    }

    @Override
    public int delete(@androidx.annotation.NonNull Uri uri, @androidx.annotation.Nullable String selection, @androidx.annotation.Nullable String[] selectionArgs) {
  //具体的逻辑
        return 0;
    }

    @Override
    public int update(@androidx.annotation.NonNull Uri uri, @androidx.annotation.Nullable ContentValues values, @androidx.annotation.Nullable String selection, @androidx.annotation.Nullable String[] selectionArgs) {
      //具体的逻辑  
      return 0;
    }
}

 

这样我们就将自己的数据共享给其他程序了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值