ContentProvider与ContentResolver

一、实验目的

contentprovider是安卓四大组件之一,使用其方法类进行数据获取;
自建一个provider,然后在另一个app中使用resolver调用这个provider。

二、核心代码

1.Contentprovider

创建数据库:MyDBHelper.java

public class MyDBHelper extends SQLiteOpenHelper {

    public MyDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        Log.d("zsy", "MyDBHelper is running...");
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table student(id integer primary key autoincrement, name varchar,age integer)");
        Log.d("zsy", "DBonCreate is running...");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

对数据库的操作在MyDAO.java中

public class MyDAO {

    private final Context context;
    private  SQLiteDatabase database;

    public MyDAO(Context context) {
        this.context = context;
        MyDBHelper dbHelper = new MyDBHelper(context,"zsyDB",null,1);
        database =dbHelper.getWritableDatabase();

    }

    public Uri DAOinsert(ContentValues values){
        long rowid = database.insert("student",null,values);

        Uri uri = Uri.parse("content://zsy.ContentProvider/student");

        Uri inserturi= ContentUris.withAppendedId(uri,rowid);

        context.getContentResolver().notifyChange(inserturi,null);
        return inserturi;

在MyContentProvider.java中进行数据获取

public class MyContentProvider extends ContentProvider {
    private MyDAO myDAO;
    public MyContentProvider() {


    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

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

    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        Context context = getContext();
        myDAO= new MyDAO(context);
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

MainActivity中new一个MyDAO对象

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyDAO myDAO= new MyDAO(this);
    }
}

ContentResolver

MainActivity中按下按钮实现数据插入

        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name","zsy");
        values.put("age","20");
        Uri uri = Uri.parse("content://zsy.ContentProvider/student");
        Button button =findViewById(R.id.button);
        button.setOnClickListener(v -> {
             resolver.insert(uri,values);
        }

三、实验结果

先运行ContentProvider出现一个空的数据库,再运行ContentResolver点击插入按钮,数据库中就会出现插入的数据
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

四、源码地址

https://gitee.com/zzzsssyyy/as_study3.git

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值