2015-12-03 65 views
0

我有一個web服務,它在成功驗證後返回用戶的配置文件。該配置文件是JSON,看起來像這樣:使用ContentProvider插入數組

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

回答

1

創建table存儲與列_idinterest 利益然後創建另一個表來存儲配置文件的映射與列_idprofile_idinterest_id

創建ContentUri利益對於每個表並相應插入。

+0

我在閱讀更多內容,以瞭解如何使用我的內容提供者插入數組。我認爲'applyBatch()'是要走的路。然而,我的'ContentProvider'是否需要重寫'applyBatch()'或默認的實現會好嗎? –

+1

'applyBatch()'的默認實現應該沒問題。你只需要在'insert'方法中正確處理所有'ContentUri' – RocketRandom