Revision as of 13:28, 3 December 2008 by seppo_fn (Talk | contribs)

Finding and viewing contacts in Java ME

From Nokia Developer Wiki
Jump to: navigation, search

Article Metadata
Tested with
Devices(s): Nokia E70, Nokia N78
Keywords: javax.microedition.pim.PIM, javax.microedition.pim.ContactList, javax.microedition.pim.Contact, javax.microedition.pim.ContactList.createContact(), javax.microedition.pim.ContactList.items()
Created: (22 Oct 2008)
Last edited: seppo_fn (03 Dec 2008)


This snippet demonstrates how to search specific contact within phone contact list.

After user enters last name, which he wants to find, application will retrieve currect contact list and search required value using method Contact.items().

Source file: FindViewContact.java

import java.util.Enumeration;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.Contact;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMItem;
import javax.microedition.pim.PIMException;
public class FindViewContact extends MIDlet implements CommandListener {
//Main form.
private Form form;
private Display display;
private Command exitCommand;
private Command searchCommand;
// Contains family name for searching.
private TextField searchName;
* Constructor. Constructs the object and initializes displayables.

public FindViewContact() {
form = new Form("Find/View PIM Contact");
// Adding command buttons
searchCommand = new Command("Search", Command.OK, 0);
exitCommand = new Command("Exit", Command.EXIT, 1);
searchName = new TextField("Enter family name:", "", 20,
display = Display.getDisplay(this);
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param cmd the command that was invoked
* @param displayable the displayable where the command was invoked

public void commandAction(Command cmd, Displayable displayable) {
if (cmd == exitCommand) {
} else if (cmd == searchCommand) {
* Called when user entered family name. Displays search results.

private void searchContact() {
ContactList contacts = null;
try {
// Retrieve the contact list.
contacts = (ContactList) PIM.getInstance().
Enumeration result = null;
//Create new temporary contact for searching.
Contact searchItem = contacts.createContact();
String[] name =
new String[contacts.stringArraySize(Contact.NAME)];
if (contacts.isSupportedArrayElement(
Contact.NAME, Contact.NAME_FAMILY)) {
name[Contact.NAME_FAMILY] = searchName.getString();
searchItem.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);
//Get enumeration of matching contacts.
result = contacts.items(searchItem);
// Clean results list.
int size = form.size();
for (int i = 1; i < size; i++) {
Contact item = null;
//If nothing was found.
if (!result.hasMoreElements())
form.append("No contacts found for: \""
+ searchName.getString()+"\"");
while (result.hasMoreElements()) {
// Display new results.
item = (Contact) result.nextElement();
form.append(" ");
} catch (PIMException e) {
//TODO: Error handling
* From MIDlet.
* Called when the MIDlet is started.

public void startApp() {
// No implementation required.
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.

public void pauseApp() {
// No implementation required.
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated

public void destroyApp(boolean unconditional) {
// No implementation required


User enters family name into text field and press «search». Search results are shown below input field.If any errors occurs, message with error description will be diplayed.

Supplementary material

You can see source file and executable application in attached zip archive. Archive is available for download at Media:Finding_and_viewing_contacts_in_J2ME.zip

121 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.