我正在尝试批量将联系人添加到联系人列表中。我可以成功地做到这一点,除非我将Starred
字段添加到批处理列表中。所以问题一定在那里。
这是我Starred
字段的 ArrayList 代码。
if(starred == true)
{
operations.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValueBackReference(RawContacts._ID, 0)
.withValue(RawContacts.STARRED, "1")
.build());
}
后来我做了:
final ContentProviderResult[] result = contentResolver.applyBatch(ContactsContract.AUTHORITY, operations);
if(result.length == operations.size())
{
Log.d(TAG, "Contact added!");
}
下面是生成的异常。我理解例外,但我看不到ops.add(...)
中发生了什么,但它肯定在那里,我只是找不到它。
02-14 00:05:54.675: E/SQLiteDatabase(1508): Error inserting account_id=2 contact_id=null _id=824 starred=1
02-14 00:05:54.675: E/SQLiteDatabase(1508): android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at com.android.providers.contacts.ContactsProvider2.insertRawContact(ContactsProvider2.java:2661)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2450)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at com.android.providers.contacts.AbstractContactsProvider.insert(AbstractContactsProvider.java:136)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2089)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at com.android.providers.contacts.AbstractContactsProvider.applyBatch(AbstractContactsProvider.java:237)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2225)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:260)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:185)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at android.os.Binder.execTransact(Binder.java:404)
02-14 00:05:54.675: E/SQLiteDatabase(1508): at dalvik.system.NativeStart.run(Native Method)
02-14 00:05:54.734: W/ContactsDatabaseHelper(1508): invalidateAllCache: [ContactsDatabaseHelper]
02-14 00:05:54.742: E/DatabaseUtils(1508): Writing exception to parcel
02-14 00:05:54.742: E/DatabaseUtils(1508): android.content.OperationApplicationException: insert failed
02-14 00:05:54.742: E/DatabaseUtils(1508): at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:216)
02-14 00:05:54.742: E/DatabaseUtils(1508): at com.android.providers.contacts.AbstractContactsProvider.applyBatch(AbstractContactsProvider.java:237)
02-14 00:05:54.742: E/DatabaseUtils(1508): at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2225)
02-14 00:05:54.742: E/DatabaseUtils(1508): at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:260)
02-14 00:05:54.742: E/DatabaseUtils(1508): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:185)
02-14 00:05:54.742: E/DatabaseUtils(1508): at android.os.Binder.execTransact(Binder.java:404)
02-14 00:05:54.742: E/DatabaseUtils(1508): at dalvik.system.NativeStart.run(Native Method)
编辑:
这是我第一次插入ArrayList
:
operations.add(ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
在错误日志的第二行,它指出PRIMARY KEY must be unique
.
在这里的主键是什么,您确定它确实是唯一的吗?
解决了我的问题。
我必须在第一次手术中出演。
喜欢这个:
operations.add(ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.withValue(RawContacts.STARRED, strStarred)
.build());
并删除它:
if(星标 == 真){
operations.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValueBackReference(RawContacts._ID, 0)
.withValue(RawContacts.STARRED, "1")
.build());
}