Create a contact list

You can create contact lists on a BlackBerry® device. Note the following about contact lists that you create:

  • Each contact list has a unique ID, which is the value that is returned by createPIMList().
  • The contact lists do not have service records and do not support wireless synchronization.
  • Applications can register to listen for changes to your contact list by invoking BlackBerryPIMList.addListener().
  1. Import the required classes and interfaces.
    import javax.microedition.pim.Contact;
    import javax.microedition.pim.PIM;
    import javax.microedition.pim.PIMException;
    import javax.microedition.pim.PIMItem;
    import net.rim.blackberry.api.pdap.BlackBerryContact;
    import net.rim.blackberry.api.pdap.BlackBerryContactList;
    import net.rim.blackberry.api.pdap.BlackBerryPIM;
    import net.rim.blackberry.api.pdap.BlackBerryPIMList;
  2. Retrieve a BlackBerryPIM object.
    BlackBerryPIM myPIM = (BlackBerryPIM) PIM.getInstance();
  3. Create the contact list.
    long listUID = myPIM.createPIMList(PIM.CONTACT_LIST, "test");
    The contact list is named using the name that you provide ("test" in the preceding example), unless there is another contact list with that name on the device, in which case a number is appended to the name to make it unique. To reference the contact list later, you should use the list's UID, which is the value that is returned by createPIMList().
  4. Optionally, populate the contact list.
    BlackBerryContactList contactList = 
       (BlackBerryContactList) myPIM.openPIMList(PIM.CONTACT_LIST, 
        PIM.READ_WRITE, listUID);
    Contact contact = contactList.createContact();
    String[] name = new String[contactList.stringArraySize(Contact.NAME)];
    name[Contact.NAME_GIVEN] = "Noha";
    name[Contact.NAME_FAMILY] = "Toma";
    contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);
    contact.commit();
  5. Close the contact list.
    contactList.close();

Code sample

BlackBerryPIM myPIM = (BlackBerryPIM) PIM.getInstance();

try 
{
   // create a contact list
   long listUID = myPIM.createPIMList(PIM.CONTACT_LIST, "test");
        			
   // add a contact to the list
   BlackBerryContactList contactList = 
      (BlackBerryContactList) myPIM.openPIMList(PIM.CONTACT_LIST, 
       PIM.READ_WRITE, listUID);
   Contact contact = contactList.createContact();
   String[] name = new String[contactList.stringArraySize(Contact.NAME)];
   name[Contact.NAME_GIVEN] = "Noha";
   name[Contact.NAME_FAMILY] = "Toma";
   contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);
   contact.addString(Contact.TEL, Contact.ATTR_HOME, "519-555-0151");
   contact.addString(Contact.TEL, Contact.ATTR_WORK, "519-555-0199");
   contact.commit();

   // close the contact list
   contactList.close();
} 
catch (PIMException e) 
{
   System.out.println(e.getMessage());
}

Was this information helpful? Send us your comments.