×
Namespaces

Variants
Actions
(Difference between revisions)

Listing contacts in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
tanjaluodes (Talk | contribs)
m
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:File Connection and PIM API (JSR-75)]][[Category:Series 40]][[Category:Symbian]][[Category:S60 3rd Edition FP2]][[Category:S60 5th Edition]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 2.0]][[Category:Nokia Belle]][[Category:PIM]][[Category:Code Examples]]
__NOEDITSECTION__
+
{{Abstract|This code example demonstrates how to list contacts in Java ME using the PIM API.}} 
  
{{KBCS}}
+
To read the contacts, use the method {{Icode|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 [http://www.developer.nokia.com/document/Java_Developers_Library_v2/GUID-654A42A3-C453-411A-A153-366C08AEF058/overview-summary.html Javadoc for JSR-75 PIM API].
{{CodeSnippet
+
 
|id=CS001395
+
{{ArticleMetaData <!-- v1.2 -->
|platform=S60 3rd Edition FP2, S60 5th Edition
+
|sourcecode= [[Media:ListContacts.zip]] [[Media:ListContacts.diff.zip]]
|devices=Nokia 5800 XpressMusic
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|category=Java ME
+
|devices= Nokia 5800 XpressMusic, Nokia C3-01, Nokia Asha 306, Nokia E7-00
|subcategory=PIM
+
|sdk= [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.1 for Java], [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 2.0 for Java (beta)], [http://www.developer.nokia.com/info/sw.nokia.com/id/ec866fab-4b76-49f6-b5a5-af0631419e9c/S60_All_in_One_SDKs.html/ Nokia Symbian SDKs]
|creationdate=June 8, 2009
+
|platform= Series 40, S60, Nokia Belle
|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()
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|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()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090506
 +
|author= [[User:Tapla]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001395
 
}}
 
}}
 
==Overview==
 
 
The following code snippet demonstrates how to list contacts in Java ME using the PIM API.
 
 
To read the contacts, use the method <tt>ContactList.items</tt>. There are several methods with this name, but in this snippet the one that has no parameters is used. For more information, see the [http://www.forum.nokia.com/document/Java_Developers_Library_v2/GUID-654A42A3-C453-411A-A153-366C08AEF058/overview-summary.html Javadoc for JSR-75 PIM API].
 
  
 
==Preconditions==
 
==Preconditions==
Line 151: Line 161:
 
* 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]].
 
* 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 general information on applying the patch, see [[Using Diffs]].
* For unpatched stub applications, see [[Example stub]].
+
* For unpatched stub applications, see [[Example app stubs with logging framework]].
 
+
[[Category:Java ME]][[Category:Code Examples]][[Category:File Connection and PIM API (JSR-75)]][[Category:Code Snippet]]
+

Revision as of 08:07, 2 October 2012

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
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 (06 May 2009)
Last edited: hamishwillee (02 Oct 2012)

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.

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

×