Fighting with this for hours and tried a whole bunch of permutations in the code. I keep getting IllegalArgumentException when I try to insert a new record.
Here is manifest Declaration:
<provider android:name="com.hakeem.model.BoxerProvider" android:exported="false"
android:readPermission="true" android:writePermission="true"
android:authorities="com.hakeem.model" />
Here is relevant parts from my Contract class:
public static final String TABLE_NAME = "boxers";
//Column Names
public static final String _ID = BaseColumns._ID;
public static final String BOXER_NAME = "boxer_name";
public static final String WEIGHT_CLASS = "weight_class";
public static final String WINS = "wins";
public static final String LOSSES ="losses";
//Uri Segments
public static final String AUTHORITY = "com.hakeem.model";
public static final String PATH = TABLE_NAME;
public static final Uri BASE_URI = Uri.parse("Content://" + AUTHORITY);
public static final Uri CONTENT_URI = BASE_URI.buildUpon().appendPath(PATH).build();
//Integer tokens for URI Matcher
public static final int BOXER = 10;
public static final int BOXERS = 20;
//Strings differents Uri paths to identify types
public static final String BOXERS_PATH = "boxers";
public static final String BOXER_PATH_ID = "boxers/#";
public static final String BOXER_TYPE = "vnd.android.cursor.dir/com.hakeem.model.boxers";
public static final String BOXER_ID_TYPE = "vnd.android.cursor.item/com.hakeem.model.boxers";
//Call function to build uri matcher
public static final UriMatcher URI_MATCHER = buildUriMatcher();
Here is the getType and insert function from by Provider Class
@Override
public String getType(Uri uri) {
int uriType = BoxerContract.URI_MATCHER.match(uri);
String sUriType;
switch(uriType){
case BoxerContract.BOXER:
sUriType = BoxerContract.BOXER_ID_TYPE;
break;
case BoxerContract.BOXERS:
sUriType = BoxerContract.BOXER_TYPE;
break;
default:
throw new IllegalArgumentException("Unknown uri: getType" + uri);
}
return sUriType;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long id;
int uriType = BoxerContract.URI_MATCHER.match(uri);
Uri insertedUri;
SQLiteDatabase boxerDB;
switch(uriType){
case BoxerContract.BOXERS:
boxerDB = dbHelper.getWritableDatabase();
id = boxerDB.insert(BoxerContract.TABLE_NAME, null, values);
break;
default:
throw new IllegalArgumentException("Unknown Uri: in insert statement" + uri);
}
insertedUri = Uri.parse(BoxerContract.TABLE_NAME + "/" + id);
return insertedUri;
}
And finally here is where I call insert in my Activity:
values.put(BoxerContract.BOXER_NAME, edBoxerName.getText().toString());
values.put(BoxerContract.WEIGHT_CLASS, edWeight.getText().toString());
values.put(BoxerContract.WINS, edWins.getText().toString());
values.put(BoxerContract.LOSSES,edLoss.getText().toString());
getContentResolver().insert(BoxerContract.CONTENT_URI, values);
Now this code in my activity's onCreate doesn't cause an error but seems to get ignored because my log statements in the query method of the provider doesn't appear in logcat.
cursor = getContentResolver().query(BoxerContract.CONTENT_URI, projection, null, null, null);
I excluded some code for brevity. Let me know if I need to post anything else.