更新联系人列表中的联系人时的IndexOutOfBoundsException - Blackberry

时间:2010-05-12 10:55:17

标签: blackberry

我正在使用的软件和模拟器版本

Blackberry Smartphone模拟器:2.13.0.65

Blackberry软件版本5.0.0_5.0.0.14

我正在考虑修改联系人。以下是我正在使用的代码段。 我在第

行获得了IndexOutOfBounds异常
  String wtel = blackBerryContact.getString(BlackBerryContact.TEL, supportedAttributes[i]);

有人可以告知这里出了什么问题。以下是代码段

.....

// Load the addressbook and let the user choose from list of contact
BlackBerryContactList contactList = (BlackBerryContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_WRITE);
PIMItem pimItem = contactList.choose();
BlackBerryContact blackBerryContact = (BlackBerryContact)pimItem;
PIMList pimList = blackBerryContact.getPIMList();

// get the supported attributes for Contact.TEL
int[] supportedAttributes = pimList.getSupportedAttributes(Contact.TEL);

Dialog.alert("Supported Attributes "+supportedAttributes.length); // gives me 8

for (int i=0; i < supportedAttributes.length;i++){
    if(blackBerryContact.ATTR_WORK == supportedAttributes[i]){
        Dialog.alert("updating Work"); // This alert is shown
        Dialog.alert("is supported "+ pimList.isSupportedAttribute(BlackBerryContact.TEL, supportedAttributes[i])+" "+pimList.getAttributeLabel(supportedAttributes[i])); // shows true and work
        String wtel = blackBerryContact.getString(BlackBerryContact.TEL, supportedAttributes[i]); // I get a IndexOutOfBounds Exception here
        if(wtel != ""){
            pimItem.removeValue(BlackBerryContact.TEL, supportedAttributes[i]);
        }
        pimItem.addString( Contact.TEL, BlackBerryContact.ATTR_WORK, number); // passing the number that has to be updated 
        if(pimItem.isModified()) {
            pimItem.commit();
            Dialog.alert("Updated Work Number");
        }
    }
}

.....

我想更新Contact.TEL字段的所有受支持属性

http://www.blackberry.com/developers/docs/5.0.0api/net/rim/blackberry/api/pdap/BlackBerryContact.html

Field       Values Per Field        Supported Attributes
-----------------------------------------------------------------------------
Contact.TEL         8         Contact.ATTR_WORK, Contact.ATTR_HOME, 
                      Contact.ATTR_MOBILE, Contact.ATTR_PAGER, 
                      Contact.ATTR_FAX, Contact.ATTR_OTHER, 
                      Contact.ATTR_HOME2, Contact.ATTR_WORK2

1 个答案:

答案 0 :(得分:0)

阅读联系电话。

int number = contact.countValues(BlackBerryContact.TEL);
            Hashtable multipleContactNumbers = new Hashtable();
            for (int i = 0; i < number; i++) {
                if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_WORK) {
                    multipleContactNumbers.put("Work: ", contact.getString(
                            BlackBerryContact.TEL, i));
                } else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_WORK2) {
                    multipleContactNumbers.put("Work 2: ", contact.getString(
                            BlackBerryContact.TEL, i));
                } else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_HOME) {
                    multipleContactNumbers.put("Home: ", contact.getString(
                            BlackBerryContact.TEL, i));
                } else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_HOME2) {
                    multipleContactNumbers.put("Home 2: ", contact.getString(
                            BlackBerryContact.TEL, i));
                } else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_MOBILE) {
                    multipleContactNumbers.put("Mobile: ", contact.getString(
                            BlackBerryContact.TEL, i));
                } else if (contact.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_OTHER) {
                    multipleContactNumbers.put("Other: ", contact.getString(
                            BlackBerryContact.TEL, i));
                }
            }

添加新的联系电话号码。

contact.addString(Contact.TEL, Contact.ATTR_HOME, "5555550100");
contact.addString(Contact.TEL, Contact.ATTR_WORK, "5555550103");
contact.addString(Contact.TEL, BlackBerryContact.ATTR_WORK2, "5555550104");

更新联系电话。

int telCount = contact.countValues(Contact.TEL);
for (int i = 0; i < telCount; ++i)
{
int telAttrs = contact.getAttributes(Contact.TEL, i);
if ((telAttrs & Contact.ATTR_MOBILE) != 0)
{
contact.setString(Contact.TEL, i, Contact.ATTR_MOBILE, "5555550109");
break;
}
}