尝试在联系人中添加值

时间:2022-04-03 15:42:28

I have this code:

我有这个代码:

String cName = name.getText().toString();
String cPhone = phone.getText().toString();
String cMail = email.getText().toString();
int cPhoneType = mContactPhoneTypes.get(mContactPhoneTypeSpinner.getSelectedItemPosition());
int cEmailType = mContactEmailTypes.get(mContactEmailTypeSpinner.getSelectedItemPosition());

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rowContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType()).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName()).build());  
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rowContactInsertIndex).withValue(ContactsContract.Data.DATA1,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,cName).build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rowContactInsertIndex).withValue(ContactsContract.Data.DATA1, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, cPhone).withValue(ContactsContract.CommonDataKinds.Phone.TYPE, cPhoneType).build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rowContactInsertIndex).withValue(ContactsContract.Data.DATA1, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Email.DATA, cMail).withValue(ContactsContract.CommonDataKinds.Email.TYPE, cEmailType).build());

try {
    ContentProviderResult[] res =  getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch(Exception e) {
    Context context = getApplicationContext();
    CharSequence txt = getString(R.string.contactCreationFailure);
    Toast.makeText(context, txt, Toast.LENGTH_LONG).show();
}

But I'm getting the following error:

但是我收到以下错误:

android.database.sqlite.SQLiteException: table raw_contacts has no column named data1: , while compiling: INSERT INTO raw_contacts(contact_id, data1, raw_contact_id) VALUES(?, ?, ?);

Any ideas? Thanks in advance.

有任何想法吗?提前致谢。

1 个解决方案

#1


0  

It would appear that you're trying to insert into the "raw contacts" table (ContactsContract.RawContacts), but using a column name (ContactsContract.Data.DATA1) related to the ContactsContracts.Data table. I'm not an Android developer, but this page on ContactsContract may be useful. Basically you want to make sure you're inserting into the right table(s) with the right column(s).

您似乎试图插入“原始联系人”表(ContactsContract.RawContacts),但使用与ContactsContracts.Data表相关的列名(ContactsContract.Data.DATA1)。我不是Android开发人员,但ContactsContract上的这个页面可能很有用。基本上你要确保用正确的列插入正确的表格。

#1


0  

It would appear that you're trying to insert into the "raw contacts" table (ContactsContract.RawContacts), but using a column name (ContactsContract.Data.DATA1) related to the ContactsContracts.Data table. I'm not an Android developer, but this page on ContactsContract may be useful. Basically you want to make sure you're inserting into the right table(s) with the right column(s).

您似乎试图插入“原始联系人”表(ContactsContract.RawContacts),但使用与ContactsContracts.Data表相关的列名(ContactsContract.Data.DATA1)。我不是Android开发人员,但ContactsContract上的这个页面可能很有用。基本上你要确保用正确的列插入正确的表格。