×
Namespaces

Variants
Actions
(Difference between revisions)

Listing contacts in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot fixing redirect link.(Moving links from forum.nokia.com TO developer.nokia.com))
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
Line 3: Line 3:
  
 
{{KBCS}}
 
{{KBCS}}
{{CodeSnippet
+
{{ArticleMetaData
 
|id=CS001395
 
|id=CS001395
 
|platform=S60 3rd Edition FP2, S60 5th Edition
 
|platform=S60 3rd Edition FP2, S60 5th Edition
Line 11: Line 11:
 
|creationdate=June 8, 2009
 
|creationdate=June 8, 2009
 
|keywords=javax.microedition.pim.Contact, javax.microedition.pim.ContactList, javax.microedition.pim.PIM, javax.microedition.pim.PIMException, javax.microedition.pim.PIM.openPIMList(), javax.microedition.pim.ContactList.items(), javax.microedition.pim.Contact.getStringArray()
 
|keywords=javax.microedition.pim.Contact, javax.microedition.pim.ContactList, javax.microedition.pim.PIM, javax.microedition.pim.PIMException, javax.microedition.pim.PIM.openPIMList(), javax.microedition.pim.ContactList.items(), javax.microedition.pim.Contact.getStringArray()
 +
 +
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
 +
|author=[[User:Tapla]]
 
}}
 
}}
  

Revision as of 12:01, 24 June 2011


Template:KBCS

Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
CompatibilityPlatform Security
Capabilities: )
Article
Keywords: javax.microedition.pim.Contact, javax.microedition.pim.ContactList, javax.microedition.pim.PIM, javax.microedition.pim.PIMException, javax.microedition.pim.PIM.openPIMList(), javax.microedition.pim.ContactList.items(), javax.microedition.pim.Contact.getStringArray()
Created: tapla (08 Jun 2009)
Last edited: hamishwillee (24 Jun 2011)

Overview

The following code snippet 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.

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.

  • The patched, executable application that can be used to test the features described in this snippet is available for download at Media:ListContacts.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:ListContacts.diff.zip.
  • For general information on applying the patch, see Using Diffs.
  • For unpatched stub applications, see Example stub.
184 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.

×