查询联系数据时,大多数事情都可以进行。我正在与StructuredName和Organization进行斗争。他们不为我工作。
如果您不介意,请看一下代码。它是手工制作的,并且删除了很多错误检查,以使其尽可能小,可以在此处发布。带有“不起作用”的注释对我不起作用。
前两个if()是onActivityResult的一部分,在该区域中,我在联系人选择的末尾接收数据。调用通过以下方式完成:
intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, Main.DIALOG_PICKCONTACT);
提前谢谢了
汉江
if (requestCode == Main.DIALOG_PICKCONTACT) {
if (resultCode == RESULT_OK) {
int i;
String s;
Cursor cursorContacts = managedQuery(intent.getData(),
null,
null,
null,
null);
if (cursorContacts != null) {
if (cursorContacts.moveToNext()) {
ContentResolver contentResolver = getContentResolver();
String contactId = cursorContacts.getString(cursorContacts.getColumnIndex(ContactsContract.Contacts._ID));
i = cursorContacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
// cursorContacts.getString(i); *** is DISPLAY_NAME
Cursor cursorEmail = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[] { contactId },
null);
if (cursorEmail != null) {
while (cursorEmail.moveToNext()) {
int type = cursorEmail.getInt(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
s = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
switch (type) {
case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
// s *** is private Email
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
// s *** is business Email
break;
}
}
cursorEmail.close();
}
Cursor cursorOrganization = contentResolver.query(
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Organization.CONTACT_ID + " = ?",
new String[] { contactId },
null);
if (cursorOrganization != null) {
if (cursorOrganization.moveToNext()) {
s = cursorOrganization.getString(cursorOrganization.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
// s *** Company name not working
s = cursorOrganization.getString(cursorOrganization.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
// s *** Function within company not working
}
cursorOrganization.close();
}
s = cursorContacts.getString(cursorContacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(s) > 0) {
Cursor cursorPhone = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] { contactId },
null);
if (cursorPhone != null) {
while (cursorPhone.moveToNext()) {
int type = cursorPhone.getInt(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
s = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
// s *** is private fax
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
// s *** is business fax
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
// s *** is private phone
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
// s *** is mobile phone
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
// s *** is business phone
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
// s *** is business mobile
break;
}
}
cursorPhone.close();
}
}
Cursor cursorStructuredName = contentResolver.query(
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?",
new String[] { contactId },
null);
if (cursorStructuredName != null) {
if (cursorStructuredName.moveToNext()) {
i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
// cursorStructuredName.getString(i); *** given name not working
i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME);
// cursorStructuredName.getString(i); *** middle name not working
i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX);
// cursorStructuredName.getString(i); *** prefix not working
i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX);
// cursorStructuredName.getString(i); *** suffix not working
}
cursorStructuredName.close();
}
Cursor cursorStructuredPostal = contentResolver.query(
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = ?",
new String[] { contactId },
null);
if (cursorStructuredPostal != null) {
while (cursorStructuredPostal.moveToNext()) {
int type = cursorStructuredPostal.getInt(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
switch (type) {
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
//s *** is private country
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
// s *** is business country
break;
}
s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
switch (type) {
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
// s *** is private city
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
// s *** is business city
break;
}
s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
switch (type) {
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
// s *** is private postcode
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
// s *** is business postcode
break;
}
s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
switch (type) {
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
// s *** is private street
break;
case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
// s *** is business street
break;
}
}
cursorStructuredPostal.close();
}
Cursor cursorWebsite = contentResolver.query(
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Website.CONTACT_ID + " = ?",
new String[] { contactId },
null);
if (cursorWebsite != null) {
while (cursorWebsite.moveToNext()) {
i = cursorWebsite.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL);
int type = cursorWebsite.getColumnIndex(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
s = cursorWebsite.getString(i);
switch (type) {
case ContactsContract.CommonDataKinds.Website.TYPE_HOME:
// s *** private url not working
break;
case ContactsContract.CommonDataKinds.Website.TYPE_WORK:
// s *** business url not working
break;
}
}
cursorWebsite.close();
}
}
cursorContacts.close();
}
}
}
最佳答案
您的条件不正确。
将此用于StructuredName:
// projection
String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME};
String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] whereParameters = new String[]{this.contact_id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
//Request
Cursor contacts = this.resolver.query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);
//Iteration
if (contacts.moveToFirst()) {
//code here
}
contacts.close();
关于android - ContactsContract.CommonDataKinds.StructuredName问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4366746/