使用 ContentProvider 插入数组

Inserting an array using ContentProvider

我有一个 Web 服务,其中 returns 成功验证后的用户配置文件。配置文件位于 JSON 中,如下所示:

{
  "first_name" : "John",
  "last_name" : "Doe",
  "interests: : ["Science","Sports"]
}  

如何使用 ContentProvider 将其保存在数据库中?

ContentValues 没有任何处理多值属性的规定。我当前的实现如下所示:

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

创建一个 table 以存储具有列 _idinterest 的兴趣 然后创建另一个 table 来存储配置文件的映射与兴趣列 _idprofile_idinterest_id

为每个 table 创建 ContentUri 并相应地插入。