The BlackBerry Developer

Thursday, June 15, 2006

Just the numbers


In my latest app I needed to add something to a menu that allowed a contact's phone numbers to be selected. It sounds easy enough but the whole PIM concept is foreign to me. Here is some of my code so that you can easily do this without wasting time. First create your menu item extending the net.rim.blackberry.api.menuitem.ApplicationMenuItem. Then implement your run method:


public Object run(Object context) {

...Code Here...
}

Next determine what the context is:


if (context == null) {

//Handle Null Context. This occurs in the
//Phone Menu if a call log is not selected.


}
else if (context instanceof BlackBerryContact) {

//Handle BlackBerryContect. This context is from the
//address book menu


..Code Here..
}
else if (context instanceof PhoneCallLog) {

//Handle PhoneCallLog. This context is
//from the Phone menu if a previous call is selected.


PhoneCallLog callLog = (PhoneCallLog) context;

String dialedPhoneNumber = callLog.getParticipant().getNumber();

..Do something with number..
}


The mildly confusing part is iterating over the phone numbers in a BlackBerry Contact. I did not find the APIs to be much help. The PIM class I am using is net.rim.blackberry.api.pdap.BlackBerryContact. I only mention this because there is also a class in the net.rim.blackberry.api.pim package with the same name. Here is the code:


BlackBerryContact blackBerryContact = (BlackBerryContact) context;

//Get the PIMList. It is useful for getting the labels for
//Each phone field

PIMList pimList = blackBerryContact.getPIMList();

if (blackBerryContact != null) {
//How many phone numbers does this contact have?
int phoneCount = blackBerryContact.countValues(Contact.TEL);

//Setup variables to hold the numbers and their labels.
String[] phoneNumbers = new String[phoneCount];
String[] labels = new String[phoneCount];

for (int i = 0; i > phoneCount; i++) {
//Fetch the phone number

String phoneNumber = blackBerryContact.getString(Contact.TEL, i);

//Determine the label for that number.
String label = pimList.getAttributeLabel(blackBerryContact.getAttributes(Contact.TEL, i));

//Add the number and label to the array.
phoneNumbers[i] = phoneNumber;
labels[i] = label + ":" + phoneNumber;
}

if (phoneCount == 0) {
..Handle the case when there is no number..
}
else if (phoneCount == 1) {
..Handle the number for
phoneNumbers[0]..
}
else {
//Create a dialog to ask the user which number they
//would like to use
int choice = Dialog.ask("Which Number?", labels, 0);

if (choice > -1 && choice < style="font-style: italic;">.. Handle the number for phoneNumbers[choice]..

}
else {
..Handle the case when the user doesn't pick a number..

}

}


So that is how you get the phone numbers from a blackBerry contact using J2ME. The other thing I had to look into was getting the actual name from the record. A getName() method would have been great but instead I had to use:


String firstName = c.getStringArray(Contact.NAME, 0)[Contact.NAME_GIVEN];
String lasttName = c.getStringArray(Contact.NAME, 0)[Contact.NAME_FAMILY];

2 Comments:

  • At 5:00 AM, Anonymous Anonymous said…

    PIMList pimList = blackBerryContact.getPIMList();

    did not work !! any suggestions !

     
  • At 5:03 AM, Anonymous Anonymous said…

    by the way how to run this code,... since its an object:
    Object run(Object context)
    should i put as the run(BlackBerryContact) as the context? in order to initialize the blackberry contact function and get the list ??
    plus what is the library of the dialog!! please help i am trying to implement it to my app
    any help or tips cchaptini@hotmail.com
    subject: MFAX help!

     

Post a Comment

<< Home