Retrieve contact information

You can retrieve information from a contact list on a BlackBerry® device by invoking one of the PIMList.items() methods. These methods return an enumeration of all the contacts in a specific contact list. You can invoke the BlackBerryContactList.items() methods to return contact groups.

  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.pdap.BlackBerryContact;
    import net.rim.blackberry.api.pdap.BlackBerryContactList;
    import net.rim.blackberry.api.pdap.BlackBerryPIMList;
    import java.util.Enumeration;
    import javax.microedition.pim.PIM;
    import javax.microedition.pim.PIMException;
    import javax.microedition.pim.PIMItem;
  2. Invoke PIM.getInstance() to retrieve a PIM instance, and invoke PIM.openPIMList() to open a contact list instance, passing in as parameters the type of list to open (PIM.CONTACT_LIST), the access mode with which to open the list (PIM.READ_WRITE, PIM.READ_ONLY, or PIM.WRITE_ONLY), and the name if you are not opening the default contact list.
    BlackBerryContactList contactList = (BlackBerryContactList) 
        PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
  3. Invoke PIMList.items() to retrieve an enumeration of items in a contact list.
    Enumeration _enum = contactList.items();
  4. Invoke one of the PIMItem getter methods to retrieve contact information.
    • To retrieve an array of fields that contain the data for a specified contact, invoke PIMItem.getFields().
    • To retrieve a String that represents the value for a specified contact field, invoke PIMItem.getString(int field, int index).
    • To retrieve a date that represents the value for a specified contact field, invoke PIMItem.getDate(int field, int index).
    while (_enum.hasMoreElements())
    {
         BlackBerryContact c = (BlackBerryContact)_enum.nextElement();
         int[] fieldIds = c.getFields();
         int id;
         for(int index = 0; index < fieldIds.length; ++index)
         {
              id = fieldIds[index];
              if(c.getPIMList().getFieldDataType(id) == BlackBerryContact.STRING)
              {
                   for(int j=0; j < c.countValues(id); ++j)
                   {
                        String value = c.getString(id, j);
                        System.out.println(c.getPIMList().getFieldLabel(id) + "=" 
                          + value);
                   }
              }
         }
    }

Was this information helpful? Send us your comments.