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

BlackBerry® World™ Development

Reply
Developer
Posts: 61
Registered: ‎09-06-2010
My Device: Not Specified
Accepted Solution

how to read the contacts from the blackberry phonebook

Hi,I want to develop an application which reads the contacts in the blackberry phonebook and displays them in a list form .To do this application which blackberry specific classes i should use like inputstreamreader,list,vectors etc.

Developer
Posts: 61
Registered: ‎09-06-2010
My Device: Not Specified

Re: how to read the contacts from the blackberry phonebook

i need to complete this application today atleast reading the contacts from the phonebook.
Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: how to read the contacts from the blackberry phonebook

Use below code

 

import java.util.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

// The pim packages.
import javax.microedition.pim.*;

public final class ContactListScreen extends MainScreen implements ListFieldCallback
{
    // Members ------------------------------------------------------------------
    private ListField _listField;   
    private ContactList _contactList;
    private Vector _contacts;
  
    // Constructors -------------------------------------------------------------    
    public ContactListScreen()
    {                
        setTitle(new LabelField("Contact List" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));              
        _listField = new ListField();        
        _listField.setCallback(this);        
        add(_listField);
        reloadContactList();
    }
    
    private boolean reloadContactList()
    {
        try
        {            
            _contactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
            
            Contact hasEmail = _contactList.createContact();
            hasEmail.addString(Contact.ORG, Contact.ATTR_NONE, "");
            hasEmail.addString(Contact.EMAIL, Contact.ATTR_HOME, "");
            
            Enumeration contactsWithEmail = _contactList.items();
            _contacts = enumToVector(contactsWithEmail);
            _listField.setSize(_contacts.size());            
            
            return true;            
        }
        catch (PIMException e)
        {
            return false;
        }
    }
    
    private Vector enumToVector(Enumeration enumeration)
    {
        Vector v = new Vector();

        if (enumeration == null)
            return v;

        while (enumeration.hasMoreElements())
        {
            v.addElement(enumeration.nextElement());
        }
        
        return v;
    }
    
    // ListFieldCallback methods ------------------------------------------------
    /**
     * @see net.rim.device.api.ui.component.ListFieldCallback#drawListRow(ListField,Graphics,int,int,int)
     */
    public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) 
    {
        if(listField == _listField && index < _contacts.size())
        {
            Contact item = (Contact)_contacts.elementAt(index);
            String[] name = item.getStringArray(Contact.NAME, 0);
            boolean found = false;
            String nameseg;
            StringBuffer sb = new StringBuffer();
            
            if((nameseg = name[Contact.NAME_PREFIX]) != null)
            {
                sb.append(nameseg);
                found = true;
            }
            
            if((nameseg = name[Contact.NAME_GIVEN]) != null)
            {
                if(found)
                {
                    sb.append(' ');
                }
                sb.append(nameseg);
                found = true;
            }
            
            if((nameseg = name[Contact.NAME_FAMILY]) != null)
            {
                if(found)
                {
                    sb.append(' ');
                }
                
                sb.append(nameseg);
            }
            
            graphics.drawText(sb.toString(), 0, y, 0, width);
        }
    }
    
    /**
     * @see net.rim.device.api.ui.component.ListFieldCallback#get(ListField , int)
     */
    public Object get(ListField listField, int index)
    {
        if(listField == _listField)
        {
            // If index is out of bounds an exception will be thrown, but that's the behaviour
            // we want in that case.
            return _contacts.elementAt(index);
        }
        
        return null;
    }
    
    /**
     * @see net.rim.device.api.ui.component.ListFieldCallback#getPreferredWidth(ListField)
     */
    public int getPreferredWidth(ListField listField) 
    {
        // Use all the width of the current LCD.
        return Display.getWidth();
    }
 
    /**
     * @see net.rim.device.api.ui.component.ListFieldCallback#indexOfList(ListField , String , int)
     */   
    public int indexOfList(ListField listField, String prefix, int start) 
    {
        return -1; // Not implemented.
    }         
}

 for details implementation of this please see PIM demo rim project.

You can find lot of  rim sample project in following path on your PC

C:\Program Files\Research In Motion\BlackBerry JDE 4.7.0\samples\com\rim\samples\device

 

Developer
Posts: 61
Registered: ‎09-06-2010
My Device: Not Specified

Re: how to read the contacts from the blackberry phonebook

UiClass

--------------------

package ContactsDemo;

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

 


/**
 *
 */
class ContactsDemo extends UiApplication
{
 public static void main(String args[])
 {
     ContactsDemo cd = new ContactsDemo();
     cd.enterEventDispatcher();
    }
   
 
  public   ContactsDemo()
  {
      pushScreen(new ContactsDemoScreen());
  }
}

 

----------------------------------------------

MainScreen class

package ContactsDemo;

import java.util.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import javax.microedition.pim.*;


public class ContactsDemoScreen extends MainScreen implements ListFieldCallback
{
  
   private ListField _listField;
   private ContactList _contactList;
    private Vector _contacts;

  
  
  
   public  ContactsDemoScreen()
   {  
   super();
   setTitle(new LabelField("Contacts List",LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH));
   _listField = new ListField();
   _listField.setCallback(this);
   add(_listField);
   reloadContactList();
}
private boolean reloadContactList()
{
    try
    {
        _contactList = (ContactList)PIM.getInstance().openPIMList(PIM.CONTACT_LIST,PIM.READ_ONLY);
        Contact hasEmail = _contactList.createContact();
        hasEmail.addString = (Contact.ORG, Contact.ATTR_NONE, "");
        hasEmail.addString(Contact.EMAIL, Contact.ATTR_HOME, "");
        _listField.setSize(_contacts.size());
        return true;
  
    }
catch(PIMException e)
{
    return false;
}
}

public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width)
{
    if(listField == _listField && index < _contacts.size())
    {
        Contact item = (Contact)_contacts.elementAt(index);
        String[] name = item.getStringArray(Contact.NAME, 0);
        boolean found = false;
        String nameseg;
        StringBuffer sb = new StringBuffer();
       
        if((nameseg == name[Contact.NAME_PREFIX]) != null)
        {
            sb.append(nameseg);
            found = true;
        }
        if((nameseg = name[Contact.NAME_GIVEN]) != null)
        {
            if(found)
            {
                sb.append(' ');
            }
            sb.append(nameseg);
        }
        graphics.drawText(sb.toString(),0,y,0,width);
    }
}

public Object get(ListField listField, int index)
{
    if(listField == _listField)
    {
        return _contacts.elementAt(index);
    }
   
    return null;
}

public int getPreferredWidth(ListField listField)
{
    return Display.getWidth();
}

public int indexOfList(ListField listField, String prefix,int start)
{
    return -1;
}
}

 

----------------------------------------

But in this below line of code compilation error i am getting that this line is not a statement

hasEmail.addString = (Contact.ORG, Contact.ATTR_NONE, "");

Developer
Posts: 61
Registered: ‎09-06-2010
My Device: Not Specified

Re: how to read the contacts from the blackberry phonebook

I manually created two new contacts in the blackberry simulator in its contacts list and saved and i need these two contacts which i have created newly to be displayed in the form of list.

For that how can i link to blackberry contact list and get the two contacts displayed by writint the program.

What is the methods and classes i have to use to retrieve the two newly created contacts and display them.

Developer
Posts: 61
Registered: ‎09-06-2010
My Device: Not Specified

Re: how to read the contacts from the blackberry phonebook

An empty list field is displaying when i run the application

Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: how to read the contacts from the blackberry phonebook

Before post I Test this code on Simulator and its work fine and fetch the first name and last name from contact.

please use above code and push directly  ContactListScreen which are post by me. 

Before check this code add at least one contact.

          I hope its work fine.

Developer
Posts: 61
Registered: ‎09-06-2010
My Device: Not Specified

Re: how to read the contacts from the blackberry phonebook

Hi Aman the code is working fine on Blackberry jde 5.0 thank you so much .

Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: how to read the contacts from the blackberry phonebook

this code also working on jde4.7 because I use jde4.7.

If your problem solved please mark as a solved.

 

Thanks  

Developer
Posts: 61
Registered: ‎09-06-2010
My Device: Not Specified

Re: how to read the contacts from the blackberry phonebook

It is showing only the first name and last name of the contacts list.Where is the option that my problem was solved.Can you explain in brief that what is actually happening in the drawlist row means you are appending to the list only the contact names thats what i understood is it correct.

Can you provide me the link on this contact list topic where they are more examples in it .