I have a web service which returns the profile of the user upon successful authentication. The profile is in JSON and looks like this:
{
"first_name" : "John",
"last_name" : "Doe",
"interests: : ["Science","Sports"]
}
How can I persist this in the database using a ContentProvider
?
ContentValues
do not have any provision for handling multi-valued attribute. My current implementation looks like this:
@Override
public Uri insert(Uri uri, ContentValues values){
switch(matcher.match(uri)){
case NEW_PROFILE:
break;
default:
return null;
}
ProfileDatabaseHelper helper = new ProfileDatabaseHelper();
// delete the existing profile first
helper.deleteUserProfile();
// then add a new profile
long id = helper.addUserProfile( values );
if( id > -1 ){
Uri insertedId = ContentUris.withAppendedId(CONTENT_URI,id);
getContext().getContentResolver().notifyChange(insertedId,null);
return insertedId;
}else{
return null;
}
}
Create a table
to store interests with columns _id
and interest
Then create another table to store mapping of profile with interests with columns _id
, profile_id
and interest_id
Create ContentUri
for each table and insert accordingly.