AS第三次作业——Content Provider

目录

一、实验内容

二、代码实现

三、结果展示

四、代码仓库


一、实验内容

存在两个app,ContentProvider和Myresolver.

其中,ContentProvider创建数据库,并提供接口,使得Myresolver可以实现对数据库的插入。

二、代码实现

          一个程序可以通过实现一个ContentProvider的抽象接口将自己的数据完全暴露出去,而且ContentProviders是以类似数据库中表的方式将数据暴露,也就是说ContentProvider就像一个“数据库”。那么外界获取其提供的数据,也就应该与从数据库中获取数据的操作基本一样,只不过是采用URI来表示外界需要访问的“数据库”。

在ContentProvider里注册

<provider
            android:name=".MyContentProvider"
            android:authorities="cjy.provider2"
            android:enabled="true"
            android:exported="true"></provider>

ContentProvider:

MyDAO:

public class MyDAO {
    private  SQLiteDatabase database;
    private MyDBhelper dBhelper;
    private Context context;

    public MyDAO(Context context) {
        this.context = context;
        //创建数据库
        dBhelper = new MyDBhelper(context, "cjyDB", null, 1);
        database = dBhelper.getWritableDatabase();
    }
    //自定义uri
    public Uri cjyInsert(){
        ContentValues value1=new ContentValues();
        value1.put("name","cjy");
        value1.put("age","20");

        Uri insertUri;
        Uri uri=Uri.parse("content://cjy.provider2/student");
        long RowId=database.insert("student",null,value1);
        insertUri= ContentUris.withAppendedId(uri,RowId);
        return insertUri;
    }

}

mycontentprovider:

    private MyDAO myDAO;
    private Context context;

    @Override
    public boolean onCreate() {
        context=this.getContext();
        myDAO=new MyDAO(context);
        return true;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        return myDAO.cjyInsert();
    }

Myresolver:

private  static final String AUTHORITY="cjy.Provider2";  
    private  static final Uri NOTIFY_URI=Uri.parse("content://"+AUTHORITY+"/student");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1=findViewById(R.id.button_i);
        Button button2=findViewById(R.id.button_u);
        Button button3=findViewById(R.id.button_q);
        Button button4=findViewById(R.id.button_d);

        ContentResolver resolver=getContentResolver();
        //把表变成uri格式的表
        Uri uri=Uri.parse("content://cjy.provider2/student");
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues value1=new ContentValues();
                value1.put("name","cjy");
                value1.put("age",20);
                resolver.insert(uri,value1);
            }
        });

三、结果展示

之前:

之后:

 

四、代码仓库

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值