You Are Here:

Community: Wiki

This page was last modified on 16 June 2009, at 10:45.

CS001394 - Deleting a contact in Java ME

From Forum Nokia Wiki



ID CS001394 Creation date June 8, 2009
Platform S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition Tested on devices Nokia N95, Nokia 6220 Classic, Nokia 5800 XpressMusic
Category Java ME Subcategory PIM


Keywords (APIs, classes, methods, functions): javax.microedition.pim.Contact, javax.microedition.pim.ContactList,

javax.microedition.pim.PIM, javax.microedition.pim.PIMException

Overview

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 CS001360 - 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.

Preconditions

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.

Source

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:

listContacts.setSelectCommand(DELETE_COMMAND);
listContacts.setCommandListener(this);
/**
* 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) {
releaseContactList();
printString(ex.toString());
return;
}
// Checking contact count
if (!contacts.hasMoreElements()) {
printString("Contact list is empty!");
}
contact = (Contact)contacts.nextElement();
while ((i != itemIndex) && (contacts.hasMoreElements())) {
contact = (Contact)contacts.nextElement();
i++;
}
if (contact == null) {
return;
}
try {
printString("Removing contact ...");
contactList.removeContact(contact);
} catch (PIMException ex) {
printString(ex.toString());
}
}
/**
* 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
display.setCurrent(mainForm);
deleteContact(i);
// Close ContactList object instance
releaseContactList();
}
}

Postconditions

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.

  • The patched, executable application that can be used to test the features described in this snippet is available for download at Media:DeleteContact.zip.
  • You can view all the changes that are required to implement the above-mentioned features. The changes are provided in unified diff and colour-coded diff (HTML) formats in Media:DeleteContact.diff.zip.
  • For general information on applying the patch, see Using Diffs.
  • For unpatched stub applications, see Example stub.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fTalkE3aE4cargeE5fscreenE5fsaverX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ