tags:

views:

81

answers:

1

Software and Simulator version i am using

Blackberry Smartphone simulator: 2.13.0.65

Blackberry software version 5.0.0_5.0.0.14

I am looking at modifying contacts. Below is the code snippet i am using. I am getting a IndexOutOfBounds Exception at line

  String wtel = blackBerryContact.getString(BlackBerryContact.TEL, supportedAttributes[i]);

Can someone advise what is going wrong here. Following is the code snippet

.....

// 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");
        }
    }
}

.....

I want to update all the supported attributes for Contact.TEL field

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
A: 

Reading contact number.

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));
                }
            }

Add new contact numbers.

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

update contact numbers.

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;
}
}
Vivart
Hi Vivartcontact.countValues(BlackBerryContact.TEL) returns the contacts that a user has currently on his card. For e.g if user does not have fax number then we cannot update fax number. Also consider a scenario where user does not have any contact numbers and only has name then contact.countValues(BlackBerryContact.TEL) returns 0 and the code will never enter the loop.What i am looking at is updating any contact number for a user. Please advise
Taha
see my updated post.
Vivart
Hi Vivart, you did not get me. Consider the following - A contact has 8 possible attributes.Has 2 nos currently, Contact.ATTR_HOME and Contact.ATTR_WORK When we say contact.countValues(Contact.TEL); it returns 2 because the contact has only 2 nos. Here i will not be able to check and add/update the other nos because it will not be available in the loop. I logic that i am trying on gets all the supported attributes for a contact the variable supportedAttributes has all the int values for the 8 attributes also "if(blackBerryContact.ATTR_WORK == supportedAttributes[i]){" is true in the for loop
Taha
I am getting an exception at String wtel = blackBerryContact.getString(BlackBerryContact.TEL, supportedAttributes[i]);
Taha