Man*_*yal 9 android android-contentresolver contactscontract android-contentprovider
目前,我在我的应用程序中显示手机的所有联系人作为自定义回收站视图.到目前为止,我在列表项视图中显示联系人的姓名,手机号码和个人资料图片,但我需要获取该联系人的所有信息,并在自定义详细信息页面中单击我的应用程序的列表项时显示该信息.
对于每个联系人,可以使用不同的信息集,例如我收到的电子邮件很少,但很少有联系人不存在.
我的问题是
如何获取给定联系人的所有信息而不会错过任何一个位.是否有一个结构可以遍历并检查每个键的值?
此外,当我在我的应用列表中填充系统联系人时,我在列表中多次找到相同的联系人.我认为这是因为在我的设备的帐户管理器中,许多帐户都注册了相同的号码,例如whatsapp,gmail.如果是这样,如何在我的列表中只显示该号码一次.
您可以执行以下操作:您将拥有自己的基础,您可以在自定义对象contact id中保存电话号码、电子邮件等详细信息,并使用该对象显示详细信息
自定义类如下:
class Contact
{
int contactId;
String Name;
ArrayList<Phone> phone;
ArrayList<Email> emails;
}
class Phone
{
String PhoneType;
String number;
}
class Email
{
String type;
String EmailId;
}
Run Code Online (Sandbox Code Playgroud)
检索详细信息并将其添加到您的自定义类中:
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
//
// Get all email addresses.
//
Cursor emails = cr.query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
String email = emails.getString(emails.getColumnIndex(Email.DATA));
int type = emails.getInt(emails.getColumnIndex(Phone.TYPE));
switch (type) {
case Email.TYPE_HOME:
// do something with the Home email here...
break;
case Email.TYPE_WORK:
// do something with the Work email here...
break;
}
}
emails.close();
}
cursor.close();
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助
| 归档时间: |
|
| 查看次数: |
226 次 |
| 最近记录: |