Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 06:17, 26 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Deleting a contact in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia N95, Nokia 6220 Classic, Nokia 5800 XpressMusic, Nokia C3-01, Nokia Asha 306, Nokia E7-00
Created: olkazmin (25 Feb 2009)
Last edited: hamishwillee (26 Jul 2013)



The following code snippet demonstrates how to delete a contact using the PIM API.

The list of contacts can be retrieved using PIM.openPIMList. For more information, refer to the code snippet Listing contacts in Java ME.

After the contact to be deleted is found, and we have a Contact reference, the ContactList.removeContact method is called to delete it.


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


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

private ContactList contactList;
private static final Command DELETE_COMMAND =
new Command("Delete", Command.ITEM, 0);

When a contact is selected in the list, delete it:

* Deletes a contact by it's index in the contact list.
* @param itemIndex index of Contact in the contact list.

private void deleteContact(int itemIndex) {
int i = 0;
Enumeration contacts = null;
Contact contact = null;
try {
printString("Getting contact list items");
contacts = contactList.items();
} catch (PIMException ex) {
// Checking contact count
if (!contacts.hasMoreElements()) {
printString("Contact list is empty!");
contact = (Contact)contacts.nextElement();
while ((i != itemIndex) && (contacts.hasMoreElements())) {
contact = (Contact)contacts.nextElement();
if (contact == null) {
try {
printString("Removing contact ...");
} catch (PIMException ex) {
* 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 == DELETE_COMMAND) {
// Get the currently selected contact
int i = listContacts.getSelectedIndex();
String item = listContacts.getString(i);
printString("Selected contact: " + item);
// Bring the main form back to the foreground
// Close ContactList object instance


The selected contact is removed from the phonebook.

See also

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 for 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 26 July 2013, at 06:17.
85 page views in the last 30 days.