Code example for MatrixCursor

Methods: newRow

0
    } 
 
    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        MatrixCursor matrix = new MatrixCursor(projection);
 
        /* 
         * Choose the table to query and a sort order based on the code returned for the incoming 
         * URI. Here, too, only the statements for table 3 are shown. 
         */ 
        switch (sUriMatcher.match(uri)) {
            // If the incoming URI was for all of the photos table 
            case PHOTOS:
                addRow(matrix, PHOTO_INDIVIDUAL_1);
                addRow(matrix, PHOTO_INDIVIDUAL_2);
                addRow(matrix, PHOTO_INDIVIDUAL_3);
                addRow(matrix, PHOTO_INDIVIDUAL_4);
                break; 
 
            // If the incoming URI was for a single row 
            case PHOTO_INDIVIDUAL_1:
                addRow(matrix, PHOTO_INDIVIDUAL_1);
                break; 
            case PHOTO_INDIVIDUAL_2:
                addRow(matrix, PHOTO_INDIVIDUAL_2);
                break; 
            case PHOTO_INDIVIDUAL_3:
                addRow(matrix, PHOTO_INDIVIDUAL_3);
                break; 
            case PHOTO_INDIVIDUAL_4:
                addRow(matrix, PHOTO_INDIVIDUAL_4);
                break; 
 
            default: 
                // If the URI is not recognized, you should do some error handling here. 
        } 
        // call the code to actually do the query 
 
        return matrix;
    } 
 
    /** 
     * Adds a single row to the Cursor. A real implementation should 
     * check the projection to properly match the columns. 
     */ 
    private void addRow(MatrixCursor matrix, int match_id) {
        switch (match_id) {
            case PHOTO_INDIVIDUAL_1:
                matrix.newRow()
                .add("content://" + PROVIDER_URI + "/photos/1")                 // uri
                .add("blah.png")                                                // displayName
                .add("content://" + PROVIDER_URI + "/photos/1/contentUri")      // contentUri
                .add("content://" + PROVIDER_URI + "/photos/1/thumbnailUri")    // thumbnailUri
                .add("image/png");                                              // contentType
                break; 
            case PHOTO_INDIVIDUAL_2:
                matrix.newRow()
                .add("content://" + PROVIDER_URI + "/photos/2")                 // uri
                .add("johannson.png")                                           // displayName
                .add("content://" + PROVIDER_URI + "/photos/2/contentUri")      // contentUri
                .add("content://" + PROVIDER_URI + "/photos/2/thumbnailUri")    // thumbnailUri
                .add("image/png");                                              // contentType
                break; 
            case PHOTO_INDIVIDUAL_3:
                matrix.newRow()
                .add("content://" + PROVIDER_URI + "/photos/3")                 // uri
                .add("planets.png")                                             // displayName
                .add("content://" + PROVIDER_URI + "/photos/3/contentUri")      // contentUri
                .add("content://" + PROVIDER_URI + "/photos/3/thumbnailUri")    // thumbnailUri
                .add("image/png");                                              // contentType
                break; 
            case PHOTO_INDIVIDUAL_4:
                matrix.newRow()
                .add("content://" + PROVIDER_URI + "/photos/4")                 // uri
                .add("galaxy.png")                                              // displayName
                .add("content://" + PROVIDER_URI + "/photos/4/contentUri")      // contentUri
                .add("content://" + PROVIDER_URI + "/photos/4/thumbnailUri")    // thumbnailUri
                .add("image/png");                                              // contentType
Experience pair programming with AI  Get Codota for Java