J2ME code for phone book

PhoneBook.java

================================================

package test2;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class PhoneBook extends MIDlet implements CommandListener, RecordComparator, ItemStateListener, RecordFilter {
private Display display;
private Form contactFrm;
private ChoiceGroup contactChoiceGrp;
private TextField searchTxtFld;
private ImageItem searchImg;
private Command exitCmd;
private Command newContactCmd;
private Command viewCmd;
private Form newContactFrm;
private TextField firstNameTxtFld;
private ImageItem mobileImg;
private TextField mobileNoTxtFld;
private Command cancelCmd;
private Command doneCmd;
private Alert doneAlert;
private RecordStore openingRecordStore;
private String firstNameStr;
private String mobileNoStr;
private String fullDetailsStr;
private int addingRecordsToRecordStoreInt;
private RecordEnumeration enumeratingRecordsInRecordStore;
private int accessNextRecordIdInt;
private String recordDataStr;
private String retreiveFirstNameStr;
private String retreiveMobileNoStr;
private Vector puttingDataIntoVector;
private String filterStr = “”;
private Form viewFrm;
private TextField viewFirstNameTxtFld;
private TextField viewMobileNoTxtFld;
private Command backCmd;
private Command updateCmd;
private int gettingSelectedIndexInt;
private int updateRecordIdInt;
private String updateFirstNameStr;
private String updateMobileNoTxtStr;
private String updateFullDetailStr;
private Alert updateAlert;
private Command deleteCmd;
private int removeRecordIdInt;
private Alert deleteAlert;

public void startApp() {
display = Display.getDisplay(this);

// see loadcontacts() method
puttingDataIntoVector = new Vector();

// “Contacts” form interface
contactFrm = new Form(“Contacts”);
contactChoiceGrp = new ChoiceGroup(“”, Choice.EXCLUSIVE);
contactFrm.append(contactChoiceGrp);
try {
searchImg = new ImageItem(“”, Image.createImage(“search.png”), Item.LAYOUT_CENTER, “SeArCh”);
} catch (IOException ex) {
ex.printStackTrace();
}
contactFrm.append(searchImg);
searchTxtFld = new TextField(“”, “”, 30, TextField.ANY);
contactFrm.append(searchTxtFld);

exitCmd = new Command(“Exit”, Command.EXIT, 7);
contactFrm.addCommand(exitCmd);
newContactCmd = new Command(“New Contact”, Command.OK, 4);
contactFrm.addCommand(newContactCmd);
viewCmd = new Command(“View”, Command.OK, 4);
contactFrm.addCommand(viewCmd);
// Relate with ItemStateListener interface and itemStateChanged() method
contactFrm.setItemStateListener(this);

contactFrm.setCommandListener(this);

// “New Contact” form interface
newContactFrm = new Form(“New Contact”);
firstNameTxtFld = new TextField(“First name:”, “”, 30, TextField.ANY);
newContactFrm.append(firstNameTxtFld);
try {
mobileImg = new ImageItem(“Mobile No:”, Image.createImage(“mobile_phone2.png”), Item.LAYOUT_2, “MoBiLe”);
} catch (IOException ex) {
ex.printStackTrace();
}
newContactFrm.append(mobileImg);
mobileNoTxtFld = new TextField(“”, “”, 20, TextField.PHONENUMBER);
newContactFrm.append(mobileNoTxtFld);

cancelCmd = new Command(“Cancel”, Command.CANCEL, 3);
newContactFrm.addCommand(cancelCmd);
doneCmd = new Command(“Done”, Command.OK, 4);
newContactFrm.addCommand(doneCmd);
newContactFrm.setCommandListener(this);

// “View Contact” form interface
viewFrm = new Form(“View Contact”);
viewFirstNameTxtFld = new TextField(“First name:”, null, 30, TextField.ANY);
viewFrm.append(viewFirstNameTxtFld);
viewMobileNoTxtFld = new TextField(“Mobile No:”, null, 20, TextField.PHONENUMBER);
viewFrm.append(viewMobileNoTxtFld);

backCmd = new Command(“Back”, Command.BACK, 2);
viewFrm.addCommand(backCmd);
updateCmd = new Command(“Update”, Command.OK, 4);
viewFrm.addCommand(updateCmd);
deleteCmd = new Command(“Delete”, Command.OK, 4);
viewFrm.addCommand(deleteCmd);
viewFrm.setCommandListener(this);

// Open a RecordStore; Next we have to add records to that RecordStore; see doneCmd
try {
openingRecordStore = RecordStore.openRecordStore(“contacts”, true);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}

loadContacts();

display.setCurrent(contactFrm);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
if(c == exitCmd){
notifyDestroyed();
}else if(c == newContactCmd){
firstNameTxtFld.setString(“”);
mobileNoTxtFld.setString(“”);
display.setCurrent(newContactFrm);
}else if(c == cancelCmd){
display.setCurrent(contactFrm);
}else if(c == doneCmd){
firstNameStr = firstNameTxtFld.getString();
mobileNoStr = mobileNoTxtFld.getString();
fullDetailsStr = firstNameStr + “*” + mobileNoStr;
byte[] dataArray = fullDetailsStr.getBytes();

// Adding record to opened record store; First we have to open record store; Next we have to enumerate those records; see loadContacts
try {
addingRecordsToRecordStoreInt = openingRecordStore.addRecord(dataArray, 0, dataArray.length);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}

loadContacts();

doneAlert = new Alert(“New contact added successfully”);
display.setCurrent(doneAlert, contactFrm);
}else if(c == viewCmd){
gettingSelectedIndexInt = contactChoiceGrp.getSelectedIndex();
Contact viewContactInstance = (Contact) puttingDataIntoVector.elementAt(gettingSelectedIndexInt);
viewFirstNameTxtFld.setString(viewContactInstance.getEncapFirstNameStr());
viewMobileNoTxtFld.setString(viewContactInstance.getEncapMobileNoStr());
display.setCurrent(viewFrm);
}else if(c == backCmd){
display.setCurrent(contactFrm);
}else if(c == updateCmd){
gettingSelectedIndexInt = contactChoiceGrp.getSelectedIndex();
Contact updateContactInstance = (Contact) puttingDataIntoVector.elementAt(gettingSelectedIndexInt);
updateRecordIdInt = updateContactInstance.getEncapAccessNextRecordIdInt();

updateFirstNameStr = viewFirstNameTxtFld.getString();
updateMobileNoTxtStr = viewMobileNoTxtFld.getString();
updateFullDetailStr = updateFirstNameStr + “*” + updateMobileNoTxtStr;
byte[] updateDataArray = updateFullDetailStr.getBytes();
try {
openingRecordStore.setRecord(updateRecordIdInt, updateDataArray, 0, updateDataArray.length);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
updateAlert = new Alert(“Update successful”);
loadContacts();
display.setCurrent(updateAlert, contactFrm);
}else if(c == deleteCmd){
gettingSelectedIndexInt = contactChoiceGrp.getSelectedIndex();
Contact deleteContactInstance = (Contact) puttingDataIntoVector.elementAt(gettingSelectedIndexInt);
removeRecordIdInt = deleteContactInstance.getEncapAccessNextRecordIdInt();
try {
openingRecordStore.deleteRecord(removeRecordIdInt);
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
deleteAlert = new Alert(“Deleted record”);
loadContacts();
display.setCurrent(deleteAlert, contactFrm);
}
}

public void loadContacts(){
// To avoid duplication when adding new contacts
contactChoiceGrp.deleteAll();
// We store data in to ChoiceGroup and Vector array at the same time. Therefor if we delete data from ChoiceGroup we have to do that for the Vector array to keep the ids equal.
puttingDataIntoVector.removeAllElements();
// Enumerating records in records store; We have to add records before enumerating
try {
// First argument is related to ItemStateListener, RecordFilter interfaces and itemStateChanged(), matches() methods
// Second argument is related to RecordComparator interface and compare() method
enumeratingRecordsInRecordStore = openingRecordStore.enumerateRecords(this, this, true);
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
// System.out.println(enumeratingRecordsInRecordStore);

// Next we have to display those enumerated records by using a while loop
while(enumeratingRecordsInRecordStore.hasNextElement()){
// It there is a record we have to access that record using the record Id
try {
accessNextRecordIdInt = enumeratingRecordsInRecordStore.nextRecordId();
} catch (InvalidRecordIDException ex) {
ex.printStackTrace();
}
// Get the record of relevant record Id and put those data in to byte array
try {
byte[] nextRecordData = openingRecordStore.getRecord(accessNextRecordIdInt);
//Convert that byte array in to String format
recordDataStr = new String(nextRecordData);
// Categorize those whole String in a meaningful way
retreiveFirstNameStr = recordDataStr.substring(0, recordDataStr.indexOf(“*”));
retreiveMobileNoStr = recordDataStr.substring(recordDataStr.indexOf(“*”)+1, recordDataStr.length());
// Those categorize data send to a different class to access in a safe way by using getter or setter methods
Contact contactClass = new Contact();
contactClass.setEncapAccessNextRecordIdInt(accessNextRecordIdInt);
contactClass.setEncapFirstNameStr(retreiveFirstNameStr);
contactClass.setEncapMobileNoStr(retreiveMobileNoStr);
// Putting data in the record store in to Vector, a growable array and ChoiceDroup at the same time
puttingDataIntoVector.addElement(contactClass);
contactChoiceGrp.append(retreiveFirstNameStr, null);

} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
}

public int compare(byte[] rec1, byte[] rec2) {
String data1 = new String(rec1);
String data2 = new String(rec2);

int x = data1.compareTo(data2);

if(x == 0){
return RecordComparator.EQUIVALENT;
}else if(x < 0){
return RecordComparator.PRECEDES;
}else{
return RecordComparator.FOLLOWS;
}
}

public void itemStateChanged(Item item) {
if(item == searchTxtFld){
filterStr = searchTxtFld.getString();
loadContacts();
}
}

public boolean matches(byte[] candidate) {
String c = new String(candidate);

if(c.startsWith(filterStr)){
return true;
}
return false;
}
}

 

================================================

Contact.java

================================================

package test2;

class Contact {
private String encapFirstNameStr;
private String encapMobileNoStr;
private int encapAccessNextRecordIdInt;

public int getEncapAccessNextRecordIdInt() {
return encapAccessNextRecordIdInt;
}

public void setEncapAccessNextRecordIdInt(int encapAccessNextRecordIdInt) {
this.encapAccessNextRecordIdInt = encapAccessNextRecordIdInt;
}

public String getEncapFirstNameStr() {
return encapFirstNameStr;
}

public void setEncapFirstNameStr(String encapFirstNameStr) {
this.encapFirstNameStr = encapFirstNameStr;
}

public String getEncapMobileNoStr() {
return encapMobileNoStr;
}

public void setEncapMobileNoStr(String encapMobileNoStr) {
this.encapMobileNoStr = encapMobileNoStr;
}

}

About AnujAroshA

An undergraduate in the stream of ICT (Information & Communication Technology). A simple person :)
This entry was posted in J2ME examples. Bookmark the permalink.

One Response to J2ME code for phone book

  1. Okumu says:

    Anuja you are crazy and to say it the least,you have a very superb brain.You are more resourceful than a book…….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s