0
votes

I'm trying to add prepopulated SQLITE tables to my content provider using SQLiteAssetHelper but the uri matcher won't match. I can access/modify the tables through standard SQL but using a cursor loader throws an exception. Here is the relevant code in the content provider/cursor loader.

//Content provider code

private PantryDbHelper dbHelper;

private static final int PANTRY = 1;
private static final int INFO = 5;

public static final String AUTHORITY = "com.battlestarmathematica.stayfresh.pantryprovider";

//path to db
public static final String URL = "content://" + AUTHORITY;
public static final Uri CONTENT_URI = Uri.parse(URL);
public static final Uri CONTENT_URI_PANTRY = Uri.withAppendedPath(CONTENT_URI,"pantry");
public static final Uri CONTENT_URI_INFO = Uri.withAppendedPath(CONTENT_URI,"info");

static final UriMatcher uriMatcher;

static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(AUTHORITY,"info",INFO);
    uriMatcher.addURI(AUTHORITY, "pantry", PANTRY);
}

public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder){
    SQLiteDatabase db = dbHelper.getReadableDatabase();

    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        switch (uriMatcher.match(uri)) {
            case PANTRY:
                builder.setTables(PantryContract.PANTRY_TABLE_NAME);
                break;
            case INFO:
                builder.setTables("info");
            default:
                throw new IllegalArgumentException("Unsupported URI " + uri);
        }

//Cursor loader code

public Loader<Cursor> onCreateLoader(int id, Bundle args){
    return new CursorLoader(
            //context
            this,
            //content URI
            PantryContentProvider.CONTENT_URI_INFO,
            //columns to return
            new String[] {"_id","itemname"},
            //selection
            null,
            //selection args
            null,
            //sort order
            "itemname");
}

I know the cursor loader works because I use the exact same code for another activity with the pantry uri and it works perfectly. When I try to load it using the info uri though I get this exception.

java.lang.IllegalArgumentException: Unsupported URI content://com.battlestarmathematica.stayfresh.pantryprovider/info

Any help would be greatly appreciated.

1
"I'm trying to add prepopulated SQLITE tables to my content provider using SQLiteAssetHelper but the uri matcher won't match" -- SQLiteAssetHelper has nothing to do with a UriMatcher. It won't match because your authority strings do not match (package.pantryprovider does not match com.battlestarmathematica.stayfresh.pantryprovider), based on your question. - CommonsWare
That's not the problem, I copy/pasted from what I sent to a friend (my fault). The authority is the same as it is in the uri. The only reason I mention SQLiteAssetHelper is because that's the only problem I can think of. The URI is exactly the same as the one that works except with a different table name. - R Kehoe

1 Answers

2
votes

You're just missing a break statement in your code.

The UriMatcher matches and the switch statement jumps to case INFO:, but since there is no break; the default: case is executed as well.

Try to replace this

        case INFO:
            builder.setTables("info");
        default:
            throw new IllegalArgumentException("Unsupported URI " + uri);

by this:

        case INFO:
            builder.setTables("info");
            break;
        default:
            throw new IllegalArgumentException("Unsupported URI " + uri);