Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 3
Registered: ‎04-25-2012
My Device: 9860
My Carrier: Airtel

Search a contact among contacts from phonebook in the application

Hi.! I have developed an application displaying the contacts from the phonebook of the device using code:

 

private Vector getContacts() {

                Vector result = new Vector();

                String mobile = "";

                try {

                        BlackBerryContactList contactList = (BlackBerryContactList) PIM

                                        .getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);

                        Enumeration enumx = contactList.items();

                        while (enumx.hasMoreElements()) {

                                BlackBerryContact c = (BlackBerryContact) enumx.nextElement();

                                String[] contact = new String[3];

                                if (contactList.isSupportedField(BlackBerryContact.NAME)) {

                                        String[] name = c.getStringArray(BlackBerryContact.NAME, 0);

                                        String firstName = name[Contact.NAME_GIVEN];

                                        String lastName = name[Contact.NAME_FAMILY];

                                        if(lastName !=null){

                                          contact[0] = firstName + " " + lastName;

                                        }

                                        else{

                                          contact[0] = firstName;

                                        }

                                        System.out.println("name----------" + contact[0]);

 

                                }

                                if (contactList.isSupportedField(BlackBerryContact.EMAIL)) {

                                        StringBuffer emails = new StringBuffer();

                                        int emailCount = c.countValues(BlackBerryContact.EMAIL);

                                        for (int i = 0; i < emailCount; i++) {

                                                String email = c.getString(BlackBerryContact.EMAIL, i);

                                                if (email != null) {

                                                        emails.append(email.trim());

 

                                                }

                                        }

                                        contact[1] = emails.toString();

                                        System.out.println("email----------" + contact[1]);

                                }

                                if (contactList.isSupportedField(BlackBerryContact.TEL)

                                                && (c.countValues(BlackBerryContact.TEL) > 0)) {

                                        // String[] mobileNumber =

                                        // c.getStringArray(BlackBerryContact.TEL, 0);

                                        int numberCount = c.countValues(BlackBerryContact.TEL);

                                        for (int i = 0; i < numberCount; i++) {

                                                // if(c.getAttributes(Contact.TEL, i) ==

                                                // Contact.ATTR_MOBILE)

                                                if (c.getAttributes(BlackBerryContact.TEL, i) == BlackBerryContact.ATTR_MOBILE) {

                                                        mobile = c.getString(BlackBerryContact.TEL, i);

                                                }

                                                else if(c.getAttributes(BlackBerryContact.TEL, i)== BlackBerryContact.ATTR_WORK){

                                                      mobile = c.getString(BlackBerryContact.TEL, i);

                                                }

                                                else{

                                                      mobile = "";

                                                }

 

                                        }

                                        contact[2] = mobile;

                                        System.out.println("mobile----------" + contact[2]);

                                }

                                result.addElement(contact);

                        }

                } catch (PIMException ex) {

                        ex.printStackTrace();

                }

                return result;

        }

 

I am using the vector returned from above code and then displaying these contacts from the vector using checkboxes for the user to select one or more contacts.

 

Now i want to search multiple contacts one at a time in the appication and then tick it in checkbox. Please help.

Highlighted
Developer
Posts: 16,838
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Search a contact among contacts from phonebook in the application

just as a warning: your code will be slow. veeeery slow.
for reasons unknown to mankind the items iteration is really slow on larger address books. internally RIM uses an efficient patricia tree, but it does not expose this.
if you want to work on the addressbook you have the choice between using a faster API than items() or to copy all content into an own collection with faster access.

check if you can use choose() instead to allow the user to select a contact.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter