移动开发技术作业3-ContentProvider

1.作业要求

请自建一个provider,然后在另一个app中使用resolver调用这个provider。

2.代码设计

本次作业要写两个程序,首先讲解CotentProvider.

MyDBHelper.java,该程序是用来创建数据库和生成数据库,当第一次建立数据库时,onCreate函数将会被调用。

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("wyh", "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("wyh", "DBonCreate is running...");

    }

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

    }
}

接着创建MyKU.java,在这个java文件中,首先利用MyDBHelper的构造函数构建了一个名为“wyhDB”的数据库,同时通过getWriableDatabase函数使得此数据库可写。此处的uri如图中所示,是一个自定义uri,对于插入时调用的函数,withAppendId函数用来插入可变的数据,如图中的uri,而AppendId用来插入不变的,固定的信息,此时第一个参数的数据类型就变成了Builder。

public class MyKU {

    private final Context context;
    private  SQLiteDatabase database;

    public MyKU(Context context) {
        this.context = context;
        MyDBHelper dbHelper = new MyDBHelper(context,"wyhDB",null,1);
        database =dbHelper.getWritableDatabase();
    }

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

        Uri uri = Uri.parse("content://wyh.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");
    }
}

以上CotentProvider程序编写完成。

然后编写CotentProvider程序, 在这里首先通过了ContentResolver类和ContentValues类创建对象,使用了put函数将需要的值填入values中,最后绑定按钮的点击事件为resolver调用insert插入函数即可。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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


        });

    }
}

3.结果展示

 

 4.仓库地址

仓库地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值