Blackberry - 如何从PIMItem获取Field Labels

时间:2010-05-06 04:55:36

标签: blackberry contacts pim

我们如何从PIMItem获取Field Labels。以下代码使用PIMList

String label = pimList.getAttributeLabel(
    blackBerryContact.getAttributes(Contact.TEL, i));

但我有PIMItem。有一个方法PIMItem.getPIMList()在下面的代码中为我返回null。 http://www.blackberry.com/developers/docs/5.0.0api/index.html上的API说“getPIMList() Gets the PIMList associated with this item.”下面是我尝试实现的示例代码 -

// Load the address Book and allow the user to select a contact
BlackBerryContactList contactList = (BlackBerryContactList) 
    PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_ONLY);
PIMItem userSelectedContact = contactList.choose();
// Now get the Field labels for contact numbers for userSelectedContact 

2 个答案:

答案 0 :(得分:1)

class Scr extends MainScreen {
    Scr() {

    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(new MenuItem("add label", 1, 1){public void run() {
            try {
                BlackBerryContactList contactList = 
                    (BlackBerryContactList) PIM.getInstance().openPIMList(
                    PIM.CONTACT_LIST, PIM.READ_ONLY);
                BlackBerryContact contact = 
                    (BlackBerryContact) contactList.choose();
                    add(new LabelField(getContactInfo(contact)));
            } catch (PIMException e) {
                e.printStackTrace();
            }

        }});
    }

    String getContactInfo(BlackBerryContact c) {
        StringBuffer result = new StringBuffer();
        result.append("Name: ");
        result.append(c.getStringArray(
                BlackBerryContact.NAME, 0)[BlackBerryContact.NAME_GIVEN]);
        result.append(" ");
        result.append(c.getStringArray(
                BlackBerryContact.NAME, 0)[BlackBerryContact.NAME_FAMILY]);
        result.append("Email: ");
        result.append("\n");
        result.append(c.getString(
                BlackBerryContact.EMAIL, BlackBerryContact.ATTR_NONE));
        return result.toString();
    }
}

答案 1 :(得分:1)

感谢Max的回复。返回的NULL问题是我的代码问题,我已经纠正。我还能够获得字段标签,但循环只检索联系人在其卡上的字段。

我希望得到Contact.TEL所有的8个标签 -

Int maxAllowed = contactList.maxValues(Contact.TEL); // Gives me 8 

所有8个标签可能都没有用于用户,例如用户可能有WORK,WORK2,HOME,HOME2和MOBILE。其他FAX,PAGER和OTHER可能没有填充我想获得所有允许的标签并更新给定的数字为空的。 我们如何检查和更新以下

Contact.ATTR_PAGER, Contact.ATTR_FAX, Contact.ATTR_OTHER

如果解释不清楚,请告知我们,或者需要更多细节。

BlackBerryContactList contactList = (BlackBerryContactList) 
    PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_WRITE);
PIMItem pimItem = contactList.choose();
BlackBerryContact blackBerryContact = (BlackBerryContact)pimItem;
PIMList pimList = blackBerryContact.getPIMList();
// To get Labels 
int phoneCount = blackBerryContact.countValues(BlackBerryContact.TEL);
String[] phoneNumbers = new String[phoneCount];
String[] labels = new String[phoneCount];
for (int i = 0; i > phoneCount; i++) {
    String phoneNumber = blackBerryContact.getString(Contact.TEL, i);
    String label = pimList.getAttributeLabel(
        blackBerryContact.getAttributes(Contact.TEL, i));
//Add the number and label to the array.
    phoneNumbers[i] = phoneNumber;
    labels[i] = label + ":" + phoneNumber;
}