0

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;
    }
}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was reading a little more into how I could insert arrays with my content provider. I think applyBatch() is the way to go. However, would my ContentProvider need to override applyBatch() or would the default implementation do just fine ?
the default implementation of applyBatch() should do fine. You just need to handle all the ContentUri correctly in the insert method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.