About this Application
We present a simple contact management application for Java enabled mobile phones. This article will be a great resource for novice J2ME programmers. This is a complete working application and full source code can be downloaded below. However, this is not an alternative to default "Contacts" application available in all mobile phones.
Contact Entity
The source code contains three files, namely Contact.java, ContactsDatabase.java and ContactsInfo.java.
The following is the data structure used to store details of a contact/person.
public class Contact
{
public String FirstName;
public String LastName;
public String MobileNumber;
public String HomeNumber;
public String OfficeNumber;
public String FaxNumber;
public String OtherNumber;
public String EMail;
}
Contacts Database
ContactsDatabase class takes care of all persistent storage activities related to the contact list.
Persistent storage in MIDP is centered around record stores. A record store is a small database file that contains pieces of data called records. J2ME contains RecordStore class for managing individual records as well as entire record stores. We use the inbuilt methods of this class for all datastore and record level operations.
A record is simply an array of bytes. So, we have to write own utility functions to convert our Contact data-structure to and from a byte-array. The following function converts a byte array into a Contact object. This function is used while reading contacts from the record-store.
private Contact deserialize(byte[] recordBytes)
{
//split the byte-array into strings
Vector recordValues = new Vector();
int startIndex = 0;
int endIndex = -1;
for(int i = 0; i < recordBytes.length; i++)
{
if(recordBytes[i] == ':')
{
endIndex = i - 1;
recordValues.addElement(new String(recordBytes, startIndex, endIndex - startIndex + 1));
startIndex = i + 1;
}
}
//build contact-object
Contact contact = new Contact();
contact.FirstName = (String)recordValues.elementAt(0);
contact.LastName = (String)recordValues.elementAt(1);
contact.MobileNumber = (String)recordValues.elementAt(2);
contact.HomeNumber = (String)recordValues.elementAt(3);
contact.OfficeNumber = (String)recordValues.elementAt(4);
contact.FaxNumber = (String)recordValues.elementAt(5);
contact.OtherNumber = (String)recordValues.elementAt(6);
contact.EMail = (String)recordValues.elementAt(7);
return(contact);
}
The following function converts a Contact object into a byte array. This function is used while adding new contact and updating details of existing contacts.
private byte[] serialize(Contact contact)
{
String recordValue = "";
recordValue += contact.FirstName + ":";
recordValue += contact.LastName + ":";
recordValue += contact.MobileNumber + ":";
recordValue += contact.HomeNumber + ":";
recordValue += contact.OfficeNumber + ":";
recordValue += contact.FaxNumber + ":";
recordValue += contact.OtherNumber + ":";
recordValue += contact.EMail + ":";
return(recordValue.getBytes());
}
ContactsInfo Application
ContactsInfo class is the main J2ME application class, containing all the GUI related stuff. The entire application is built with these controls: List, Form and TextField. The first and main screen of the application displays the list of contacts saved by the user. The user can invoke a menu containing the options to add new contact, to edit an existing contact, to delete an existing contact and to view details of an existing contact. The following is the screenshot in J2ME simulator.
This class will be very helpful for understanding a basic MIDlet life cycle, using Display class to switch between various forms in the application, event handling with commands for responding to user actions, getting user input in forms and showing alert & confirmation messages.
The application is developed with J2SDK 1.4.2 build 19 and J2ME Wireless Toolkit 2.2; the application will be compatible with all Java enabled mobile phones and future MIDlet versions.
Downloads for this article
File |
Language |
Tools |
ContactsInfo-J2ME-Source 3.61 kb (376 downloads) |
J2ME |
J2ME WTK 2.2 |