×
Namespaces

Variants
Actions

Listing contacts in Java ME

From Nokia Developer Wiki
Jump to: navigation, search

This code example demonstrates how to list contacts in Java ME using the PIM API.

To read the contacts, use the method ContactList.items. There are several methods with this name, but in this snippet the one that has no parameters is used. For more information, see the Javadoc for JSR-75 PIM API.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic, Nokia C3-01, Nokia Asha 306, Nokia E7-00
CompatibilityArticle
Created: tapla (06 May 2009)
Last edited: hamishwillee (22 Jul 2013)

Contents

Preconditions

In order to test this snippet there should be at least one contact in the database. The application also needs permission to read and write PIM database data.

Source

private List listContacts;
 
/**
* Holds reference to the currently opened contact list.
*/

private ContactList contactList;
 
private static final Command BACK_COMMAND =
new Command("Back", Command.BACK, 0);
/**
* Instantiates a listContacts.
*/

private void setupContactList() {
listContacts = new List("Contact list", Choice.IMPLICIT);
listContacts.addCommand(BACK_COMMAND);
listContacts.setCommandListener(this);
}
/**
* Executes the snippet.
*/

private void executeSnippet() {
openContactList();
}
 
/**
* Fills listContacts fith data read from PIM database and brings
* the list to the foreground.
*/

private void openContactList() {
fillContactList();
display.setCurrent(listContacts);
printString("Done");
}
 
/**
* Reads contacts list from PIM database 'Contacts' and fills listContacts
* with contact 'name' field value.
*/

private void fillContactList() {
try {
printString("Opening contact list ...");
contactList = (ContactList)PIM.getInstance().openPIMList(
PIM.CONTACT_LIST, PIM.READ_WRITE);
 
} catch (PIMException ex) {
printString(ex.toString());
return;
}
Enumeration contacts = null;
Contact contact = null;
try {
printString("Getting contact list items");
contacts = contactList.items();
} catch (PIMException ex) {
releaseContactList();
printString(ex.toString());
return;
}
// Checking contact count
if (!contacts.hasMoreElements()) {
printString("Contact list IS empty!");
}
if (listContacts.size() > 0) {
listContacts.deleteAll();
}
while (contacts.hasMoreElements()) {
contact = (Contact)contacts.nextElement();
String contactInfo = contact.getStringArray(Contact.NAME,
Contact.ATTR_NONE)[Contact.NAME_GIVEN];
if (contactInfo != null) {
listContacts.append(contactInfo, null);
}
}
}
 
/**
* Closes the contact list if one is opened.
*/

private void releaseContactList() {
if (contactList != null) {
try {
printString("Closing contact list");
contactList.close();
} catch (PIMException ex) {
printString(ex.toString());
}
}
contactList = null;
}
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/

public void commandAction(Command command, Displayable displayable) {
if (command == BACK_COMMAND) {
display.setCurrent(mainForm);
// Close ContactList object instance
releaseContactList();
}
}

Postconditions

The contacts in the phonebook are listed on the screen.

Supplementary material

This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful to developers. The version of the Java ME stub application used as a template in this snippet is v1.1.

This page was last modified on 22 July 2013, at 13:54.
169 page views in the last 30 days.