Quick Search for:  in language:    
Mobile,phones,enable,enter,list,phone,numbers
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Java/ Javascript Stats

 Code: 220,465. lines
 Jobs: 92. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Java/ Javascript.
Click here to see a screenshot of this code!vok - The vocabulary trainer
By Thorsten Stärk on 1/7

(Screen Shot)

Java, Calculator
By Rockwell on 1/4


Eatable Interface
By Rockwell on 1/4


Superclass Person
By Rockwell on 1/4


Draws Cube Function
By Rockwell on 1/4


Rectangle Class
By Rockwell on 1/4


Find Number of Upper and Lower Case Letters in a Command Line Argument String
By Rockwell on 1/4


anagrams
By Rockwell on 1/4


Text Reader with Tokenizer
By Rockwell on 1/4


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

J2ME Contacts for Mobile's, PDA & other Devices

Print
Email
 
VB icon
Submitted on: 8/1/2002 11:30:02 AM
By: Bhushan.  
Level: Advanced
User Rating: By 26 Users
Compatibility:Java (JDK 1.2)

Users have accessed this code 8351 times.
 

(About the author)
 
     Mobile phones enable you to enter a list of phone numbers that You commonly use, along with the name of the person Associated with the number. Although this primitive contact list is useful for Making calls for familiar friends and business associates.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: J2ME Contacts for Mobile's, PDA
//     & other Devices
// Description:Mobile phones enable you 
//     to enter a list of phone numbers that 
You commonly use, along with the name of the person 
Associated with the number.
Although this primitive contact list is useful for 
Making calls for familiar friends and business associates.
// By: Bhushan.
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=3083&lngWId;=2//for details.//**************************************
//     

/* Save this file Contacts.java
* Start the code 
*
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
    public class Contacts extends MIDlet implements CommandListener {
    private Command exitCommand, addCommand, backCommand, saveCommand,
    deleteCommand;
    private Display display;
    private List mainScreen;
    private Form contactScreen;
    private TextField nameField, companyField, phoneField, faxField;
    private ChoiceGroup contactType;
    private boolean editing = false;
    private ContactDB db = null;
    private Vector contactIDs = new Vector();
    private Image[] typeImages;
        public Contacts() {
        // Get the Display object for the MIDlet
        //     
        display = Display.getDisplay(this);
        // Create the commands
        exitCommand = new Command("Exit", Command.EXIT, 2);
        addCommand = new Command("Add", Command.SCREEN, 3);
        backCommand = new Command("Back", Command.BACK, 2);
        saveCommand = new Command("Save", Command.OK, 3);
        deleteCommand = new Command("Delete", Command.SCREEN, 3);
        // Create the main screen
        mainScreen = new List("Contacts", List.IMPLICIT);
        // Set the Exit and Add commands for the
        //     main screen
        mainScreen.addCommand(exitCommand);
        mainScreen.addCommand(addCommand);
        mainScreen.setCommandListener(this);
        // Create the contact screen
        contactScreen = new Form("Contact Info");
        nameField = new TextField("Name", "", 30, TextField.ANY);
        contactScreen.append(nameField);
        companyField = new TextField("Company", "", 15, TextField.ANY);
        contactScreen.append(companyField);
        phoneField = new TextField("Phone", "", 10, TextField.PHONENUMBER);
        contactScreen.append(phoneField);
        faxField = new TextField("Fax", "", 10, TextField.PHONENUMBER);
        contactScreen.append(faxField);
        String[] choices = { "Personal", "Business", "Family", "Other" };
        contactType = new ChoiceGroup("Type", Choice.EXCLUSIVE, choices, null);
        contactScreen.append(contactType);
        // Set the Back, Save, and Delete comman
        //     ds for the contact screen
        contactScreen.addCommand(backCommand);
        contactScreen.addCommand(saveCommand);
        contactScreen.addCommand(deleteCommand);
        contactScreen.setCommandListener(this);
        // Load the type images

            try {
            typeImages = new Image[4];
            typeImages[0] = Image.createImage("/Personal.png");
            typeImages[1] = Image.createImage("/Business.png");
            typeImages[2] = Image.createImage("/Family.png");
            typeImages[3] = Image.createImage("/Other.png");
        }

catch (IOException e) { System.err.println("EXCEPTION: Failed loading images!"); }
// Open the contact database try { db = new ContactDB("contacts"); }
catch(Exception e) { System.err.println("EXCEPTION: Problem opening the database."); }
// Read through the database and build a // list of record IDs RecordEnumeration records = null; try { records = db.enumerateContactRecords(); while(records.hasNextElement()) contactIDs.addElement(new Integer(records.nextRecordId())); }
catch(Exception e) { System.err.println("EXCEPTION: Problem reading the contact records."); }
// Read through the database and fill th // e contact list records.reset(); try { while(records.hasNextElement()) { Contact contact = new Contact(records.nextRecord()); mainScreen.append(contact.getName(), typeImages[contact.getType()]); }
}
catch(Exception e) { System.err.println("EXCEPTION: Problem reading the contact records."); }
}
public void startApp() throws MIDletStateChangeException { // Set the current display to the main s // creen display.setCurrent(mainScreen); }
public void pauseApp() { }
public void destroyApp(boolean unconditional) { // Close the contact database try { db.close(); }
catch(Exception e) { System.err.println("EXCEPTION: Problem closing the database."); }
}
public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); }
else if (c == addCommand) { // Clear the contact fields nameField.setString(""); companyField.setString(""); phoneField.setString(""); faxField.setString(""); contactType.setSelectedIndex(0, true); // Remove the Delete command from the co // ntact screen contactScreen.removeCommand(deleteCommand); editing = false; // Set the current display to the contac // t screen display.setCurrent(contactScreen); }
else if (c == List.SELECT_COMMAND) { // Get the record ID of the currently se // lected contact int index = mainScreen.getSelectedIndex(); int id = ((Integer)contactIDs.elementAt(index)).intValue(); // Retrieve the contact record from the // database Contact contact = db.getContactRecord(id); // Initialize the contact fields nameField.setString(contact.getName()); companyField.setString(contact.getCompany()); phoneField.setString(contact.getPhone()); faxField.setString(contact.getFax()); contactType.setSelectedIndex(contact.getType(), true); // Add the Delete command to the contact // screen contactScreen.addCommand(deleteCommand); editing = true; // Set the current display to the contac // t screen display.setCurrent(contactScreen); }
else if (c == deleteCommand) { // Get the record ID of the currently se // lected contact int index = mainScreen.getSelectedIndex(); int id = ((Integer)contactIDs.elementAt(index)).intValue(); // Delete the contact record db.deleteContactRecord(id); contactIDs.removeElementAt(index); mainScreen.delete(index); // Set the current display back to the m // ain screen display.setCurrent(mainScreen); }
else if (c == backCommand) { // Set the current display back to the m // ain screen display.setCurrent(mainScreen); }
else if (c == saveCommand) { if (editing) { // Get the record ID of the currently se // lected contact int index = mainScreen.getSelectedIndex(); int id = ((Integer)contactIDs.elementAt(index)).intValue(); // Create a record for the contact and s // et it in the database Contact contact = new Contact(nameField.getString(), companyField.getString(), phoneField.getString(), faxField.getString(), contactType.getSelectedIndex()); db.setContactRecord(id, contact.pack()); mainScreen.set(index, contact.getName(), typeImages[contact.getType()]); }
else { // Create a record for the contact and a // dd it to the database Contact contact = new Contact(nameField.getString(), companyField.getString(), phoneField.getString(), faxField.getString(), contactType.getSelectedIndex()); contactIDs.addElement(new Integer(db.addContactRecord(contact.pack()))); mainScreen.append(contact.getName(), typeImages[contact.getType()]); }
// Set the current display back to the m // ain screen display.setCurrent(mainScreen); }
}
}
/* END OF CODE Contacts.java * */ /* Sava this file name as Contact.java * Start the code */ import java.util.*; public class Contact { private String name, company, phone, fax; private int type; public Contact(String n, String c, String p, String f, int t) { name = n; company = c; phone = p; fax = f; type = t; }
public Contact(byte[] data) { unpack(new String(data)); }
public void unpack(String data) { int start = 0, end = data.indexOf(';'); name = data.substring(start, end); start = end + 1; end = data.indexOf(';', start); company = data.substring(start, end); start = end + 1; end = data.indexOf(';', start); phone = data.substring(start, end); start = end + 1; end = data.indexOf(';', start); fax = data.substring(start, end); start = end + 1; type = Integer.parseInt(data.substring(start, data.length())); }
public String pack() { return (name + ';' + company + ';' + phone + ';' + fax + ';' + ((String)Integer.toString(type))); }
public String getName() { return name; }
public String getCompany() { return company; }
public String getPhone() { return phone; }
public String getFax() { return fax; }
public int getType() { return type; }
}
/* End of code Contact.java * */ /* Save this file name as ContactDB.java * */ import javax.microedition.rms.*; import java.util.Enumeration; import java.util.Vector; import java.io.*; public class ContactDB { RecordStore recordStore = null; public ContactDB(String name) { // Open the record store using the speci // fied name try { recordStore = open(name); }
catch(RecordStoreException e) { e.printStackTrace(); }
}
public RecordStore open(String fileName) throws RecordStoreException { return RecordStore.openRecordStore(fileName, true); }
public void close() throws RecordStoreNotOpenException, RecordStoreException { // If the record store is empty, delete // the file if (recordStore.getNumRecords() == 0) { String fileName = recordStore.getName(); recordStore.closeRecordStore(); recordStore.deleteRecordStore(fileName); }
else { // Otherwise, close the record store recordStore.closeRecordStore(); }
}
public Contact getContactRecord(int id) { // Get the contact record from the recor // d store try { return (new Contact(recordStore.getRecord(id))); }
catch (RecordStoreException e) { e.printStackTrace(); }
return null; }
public synchronized void setContactRecord(int id, String record) { // Convert the string record to an array // of bytes byte[] bytes = record.getBytes(); // Set the record in the record store try { recordStore.setRecord(id, bytes, 0, bytes.length); }
catch (RecordStoreException e) { e.printStackTrace(); }
}
public synchronized int addContactRecord(String record) { // Convert the string record to an array // of bytes byte[] bytes = record.getBytes(); // Add the byte array to the record stor // e try { return recordStore.addRecord(bytes, 0, bytes.length); }
catch (RecordStoreException e) { e.printStackTrace(); }
return -1; }
public synchronized void deleteContactRecord(int id) { // Delete the contact record from the re // cord store try { recordStore.deleteRecord(id); }
catch (RecordStoreException e) { e.printStackTrace(); }
}
public synchronized RecordEnumeration enumerateContactRecords() throws RecordStoreNotOpenException { return recordStore.enumerateRecords(null, null, false); }
}
/* End of code ContactDB.java * */


Other 17 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
8/1/2002 11:39:35 AM:Henrry
Editing member variable necessary?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/1/2002 11:43:38 AM:Bhushan Paranjpe
Save command doesn’t inherently know 
whether the user is
Saving as edited 
contact or adding a new contact
Editor 
contacts must set an existing record in 
the record store 
Whereas a new 
contact must be added to the record 
store.
So it is very important for the 
save command code to know
Whether a 
contact is be edited or added to the 
record store.
This knowledge is 
provided to the save command code by 
the editing
Member variable.
Which 
serves as a flag that is set when a 
contact is edited.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/2/2002 9:03:37 AM:Ashlley
Good Code,
Good Work in J2ME
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/10/2002 1:57:35 AM:Jonathan
hi bhushan,
code look like very 
cretical,
can you simplify it,
but by 
the way well deserved code
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Java/ Javascript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.