黑莓 - 获取联系人列表

时间:2010-01-20 06:10:52

标签: blackberry addressbook contacts

我想从黑莓JDE 4.7的联系人列表中获取所有名称及其相应电子邮件地址的列表,任何人都可以帮助获取上述内容的代码。

提前致谢...

1 个答案:

答案 0 :(得分:11)

试试这段代码:

public Scr() {
    Vector v = getContacts();
    Enumeration iterator = v.elements();
    while (iterator.hasMoreElements()) {
        String[] contact = (String[]) iterator.nextElement();
        for (int i = 0; i < contact.length; i++)
            add(new LabelField(contact[i]));
    }

}

private Vector getContacts() {
    Vector result = new Vector();
    try {
        BlackBerryContactList contactList = (BlackBerryContactList) PIM
                .getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
        Enumeration enumx = contactList.items();
        while (enumx.hasMoreElements()) {
            BlackBerryContact c = (BlackBerryContact) enumx.nextElement();
            String[] contact = new String[2];
            if (contactList.isSupportedField(BlackBerryContact.NAME)) {
                String[] name = c.getStringArray(BlackBerryContact.NAME, 0);
                String firstName = name[Contact.NAME_GIVEN];
                String lastName = name[Contact.NAME_FAMILY];
                contact[0] = firstName + " " + lastName;
            }
            if (contactList.isSupportedField(BlackBerryContact.EMAIL)) {
                StringBuffer emails = new StringBuffer();
                int emailCount = c.countValues(BlackBerryContact.EMAIL);
                for (int i = 0; i < emailCount; i++) {
                    String email = c.getString(BlackBerryContact.EMAIL, i);
                    if (email != null) {
                        emails.append(email.trim());
                        emails.append("; ");
                    }
                }
                contact[1] = emails.toString();
            }
            result.addElement(contact);
        }
    } catch (PIMException ex) {
        ex.printStackTrace();
    }
    return result;
}