ContentProvider, one of the four components

Content provider: provider of data to other programs
Content resolver: read or operate the data of corresponding program through content provider

Content provider steps:
Create a new class to inherit ContentProvider and create a content provider to implement its methods
Or create new – Other – Content Provider under the package

Create a static code block, create a UriMatcher path matcher, and call the addURI() method to add matching rules
A complete uri is content://com.example.aap.provider/query
uri is composed of authority and path. The third parameter is a match code

 static {
        //Create a UriMatcher path matcher
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        
        //Call addURI() method to add matching rule
        uriMatcher.addURI("com.example.mycontent.provider", "query", QUERYTABLE);
        uriMatcher.addURI("com.example.mycontent.provider", "insert", INSERTTABLE);
        uriMatcher.addURI("com.example.mycontent.provider", "delete", DELETETABLE);
        uriMatcher.addURI("com.example.mycontent.provider", "update", UPDATETABLE);
    }

Initializes the content provider, which calls the database creation and upgrade

    //
    @Override
    public boolean onCreate() {
        myDatabase = new MyDatabase(getContext());
        db = myDatabase.getReadableDatabase();
        return true;
    }

Because it provides data for the provider of other programs, it directly returns

Add data

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = uriMatcher.match(uri);
        //Determine whether the transmitted uri corresponds to the matching code
        if (code == INSERTTABLE) {
            long insert = db.insert("meno", null, values);
            Uri uri1 = Uri.parse(String.valueOf(insert));
            
            //The return value represents the id of the newly added data
            return uri1;
        } else {
            throw new UnsupportedOperationException("Path mismatch");
        }
    }

Update data

   @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int code = uriMatcher.match(uri);
        if (code == UPDATETABLE) {
            int update = db.update("meno", values, selection, selectionArgs);
            //The return value represents the number of rows affected
            return update;
        }
        throw new UnsupportedOperationException("Path mismatch");
    }

Delete data

   @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code = uriMatcher.match(uri);
        if (code == DELETETABLE) {
            int delete = db.delete("meno", selection, selectionArgs);
            //The return value represents the number of rows deleted
            return delete;
        }
        throw new UnsupportedOperationException("Path mismatch");
    }

Query data

  @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int code = uriMatcher.match(uri);
        if (code == QUERYTABLE) {
            //Path match successful
            Cursor cursor = db.query("meno", projection, selection, selectionArgs, null, null, sortOrder);
            return cursor;
        } else {
            throw new UnsupportedOperationException("Path mismatch");
        }
    }

Content resolver

Content resolver add

                Uri uri1 = Uri.parse("content://com.example.mycontent.provider/insert");
                ContentValues values = new ContentValues();
                values.put("name", "Fox");
                values.put("age", 21);
                Uri insert = getContentResolver().insert(uri1, values);
                Toast.makeText(this, "Added" + insert + "That's ok", Toast.LENGTH_SHORT).show();

Content resolver update

                Uri uri2 = Uri.parse("content://com.example.mycontent.provider/update");
                ContentValues values1 = new ContentValues();
                values1.put("name", "Panda");
                int update = getContentResolver().update(uri2, values1, "name=?", new String[]{"Fox"});
                Toast.makeText(this, "Updated" + update + "That's ok", Toast.LENGTH_SHORT).show();

Content resolver delete

                Uri uri3 = Uri.parse("content://com.example.mycontent.provider/delete");
                int delete = getContentResolver().delete(uri3, null, null);
                Toast.makeText(this, "Deleted" + delete + "That's ok", Toast.LENGTH_SHORT).show();

Content resolver query

Uri uri4 = Uri.parse("content://com.example.mycontent.provider/query");
                Cursor cursor = getContentResolver().query(uri4, null, null, null, null);
                if (cursor != null && cursor.getCount() > 0) {
                    while (cursor.moveToNext()) {
                        String name = cursor.getString(1);
                        int age = cursor.getInt(2);
                        Toast.makeText(this,name+age,Toast.LENGTH_SHORT).show();
                    }
                }

Keywords: Database

Added by hamboy on Fri, 20 Dec 2019 20:53:16 +0200