尝试在联系人中添加值

时间:2011-05-09 07:16:03

标签: java android

我有这段代码:

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

但我收到以下错误:

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(?, ?, ?);

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

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