×
Namespaces

Variants
Actions
(Difference between revisions)

Finding and viewing contacts in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix metadata)
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:Code Examples]][[Category:PIM]][[Category:File Connection and PIM API (JSR-75)]][[Category:Code Snippet]][[Category:Series 40]][[Category:Symbian]][[Category:S60 3rd Edition (initial release)]][[Category:S60 3rd Edition FP1]][[Category:S60 3rd Edition FP2]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 2.0]][[Category:Nokia Belle]]
__NOEDITSECTION__
+
{{ArticleMetaData <!-- v1.2 -->
{{CodeSnippet
+
|sourcecode= [[Media:Finding and viewing contacts in J2ME.zip]]
|id=&nbsp;
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|platform=S60 3rd Edition, S60 3rd Edition, FP1, S60 3rd Edition, FP2
+
|devices= Nokia E70, Nokia N78, Nokia C3-01, Nokia Asha 306, Nokia E7-00
|devices=Nokia E70, Nokia N78
+
|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 2.0], [http://www.developer.nokia.com/info/sw.nokia.com/id/ec866fab-4b76-49f6-b5a5-af0631419e9c/S60_All_in_One_SDKs.html/ Nokia Symbian SDKs]
|category=Java ME
+
|platform= Series 40, S60, Nokia Belle
|subcategory=Phonebook/Contacts DB
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|creationdate=October 22, 2008
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|keywords=javax.microedition.pim.PIM, javax.microedition.pim.ContactList, javax.microedition.pim.Contact, javax.microedition.pim.ContactList.createContact(), javax.microedition.pim.ContactList.items()
+
|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.PIM, javax.microedition.pim.ContactList, javax.microedition.pim.Contact, javax.microedition.pim.ContactList.createContact(), javax.microedition.pim.ContactList.items()
 +
|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= 20081127
 +
|author= [[User:Vltsoy]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= Phonebook/Contacts DB
 +
|id= CS001184
 
}}
 
}}
  
 
==Overview==
 
==Overview==
  
This snippet demonstrates how to search specific contact within phone contact list.
+
This snippet demonstrates how to search for a specific contact in phone contact list.
  
After user enters last name, which he wants to find, application will retrieve currect contact list and search required value using method <tt>Contact.items()</tt>.
+
After user has enterd the last name of the contact he or she wants to find, the application retrieves the currect contact list and searches for the required value using the method {{Icode|Contact.items()}}.
  
 
==Source file: FindViewContact.java==
 
==Source file: FindViewContact.java==
Line 164: Line 179:
 
==Postconditions==
 
==Postconditions==
  
User enters family name into text field and press «search». Search results are shown below input field.If any errors occurs, message with error description will be diplayed.
+
After the user has enterd the last name family name of a contact into a text field and pressed «search», the search results are shown in below the input field. If any errors occurs, a message describing the error is diplayed.
  
 
==Supplementary material==
 
==Supplementary material==
  
You can see source file and executable application in attached zip archive. Archive is available for download at [[Media:Finding_and_viewing_contacts_in_J2ME.zip]]
+
The source file and the executable application are available for download at [[Media:Finding and viewing contacts in J2ME.zip]].
 
+
[[Category:Java ME]][[Category:Code Examples]][[Category:PIM]]
+
[[Category:File Connection and PIM API (JSR-75)]]
+

Revision as of 07:57, 2 October 2012

Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia N78, Nokia C3-01, Nokia Asha 306, Nokia E7-00
CompatibilityArticle
Keywords: javax.microedition.pim.PIM, javax.microedition.pim.ContactList, javax.microedition.pim.Contact, javax.microedition.pim.ContactList.createContact(), javax.microedition.pim.ContactList.items()
Created: vltsoy (27 Nov 2008)
Last edited: hamishwillee (02 Oct 2012)

Contents

Overview

This snippet demonstrates how to search for a specific contact in phone contact list.

After user has enterd the last name of the contact he or she wants to find, the application retrieves the currect contact list and searches for the required value using the method Contact.items().

Source file: FindViewContact.java

import java.util.Enumeration;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.Contact;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMItem;
import javax.microedition.pim.PIMException;
 
public class FindViewContact extends MIDlet implements CommandListener {
 
//Main form.
private Form form;
private Display display;
private Command exitCommand;
private Command searchCommand;
// Contains family name for searching.
private TextField searchName;
 
/**
* Constructor. Constructs the object and initializes displayables.
*/

public FindViewContact() {
form = new Form("Find/View PIM Contact");
 
// Adding command buttons
searchCommand = new Command("Search", Command.OK, 0);
exitCommand = new Command("Exit", Command.EXIT, 1);
form.addCommand(searchCommand);
form.addCommand(exitCommand);
 
searchName = new TextField("Enter family name:", "", 20,
TextField.ANY);
 
form.append(searchName);
 
 
form.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(form);
}
 
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param cmd the command that was invoked
* @param displayable the displayable where the command was invoked
*/

public void commandAction(Command cmd, Displayable displayable) {
if (cmd == exitCommand) {
notifyDestroyed();
} else if (cmd == searchCommand) {
searchContact();
}
}
 
/**
* Called when user entered family name. Displays search results.
*/

private void searchContact() {
ContactList contacts = null;
try {
// Retrieve the contact list.
contacts = (ContactList) PIM.getInstance().
openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
Enumeration result = null;
 
//Create new temporary contact for searching.
Contact searchItem = contacts.createContact();
String[] name =
new String[contacts.stringArraySize(Contact.NAME)];
if (contacts.isSupportedArrayElement(
Contact.NAME, Contact.NAME_FAMILY)) {
name[Contact.NAME_FAMILY] = searchName.getString();
}
searchItem.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name);
 
//Get enumeration of matching contacts.
result = contacts.items(searchItem);
 
// Clean results list.
int size = form.size();
for (int i = 1; i < size; i++) {
form.delete(1);
}
Contact item = null;
//If nothing was found.
if (!result.hasMoreElements())
{
form.append("No contacts found for: \""
+ searchName.getString()+"\"");
}
while (result.hasMoreElements()) {
// Display new results.
item = (Contact) result.nextElement();
form.append(item.getStringArray(Contact.NAME,
Contact.ATTR_NONE)[Contact.NAME_GIVEN]);
form.append(" ");
form.append(item.getStringArray(Contact.NAME,
Contact.ATTR_NONE)[Contact.NAME_FAMILY]);
form.append("\n");
}
contacts.removeContact(searchItem);
} catch (PIMException e) {
//TODO: Error handling
}
}
 
/**
* From MIDlet.
* Called when the MIDlet is started.
*/

public void startApp() {
// No implementation required.
}
 
/**
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.
*/

public void pauseApp() {
// No implementation required.
}
 
/**
* From MIDlet.
* Called to signal the MIDlet to terminate.
* @param unconditional whether the MIDlet has to be unconditionally
* terminated
*/

public void destroyApp(boolean unconditional) {
// No implementation required
}
}

Postconditions

After the user has enterd the last name family name of a contact into a text field and pressed «search», the search results are shown in below the input field. If any errors occurs, a message describing the error is diplayed.

Supplementary material

The source file and the executable application are available for download at Media:Finding and viewing contacts in J2ME.zip.

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

×