Populate Address Book
/* * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. */ import javax.microedition.midlet.*; import javax.microedition.rms.*; /** * Class to create an example address book and populate it * with some records */ public class PopulateAddressBook extends MIDlet { private final String personalNames[] = { "John", "Zach", "2225556669", "Mark", "Lynn", "5125551212", "Joy", "Beth", "2705551234", "Abby", "Lynn", "4085558566", "Ted", "Alan", "4125552235", "Sterling", "Wincle", "9995559111", "Deborah", "Elaine", "4445552323", "Suzanne", "Melissa","5125556064", "Frank", "Kenneth", "7775551212", "Dwight", "Poe", "1115557234", "Laura", "Beth", "2055558888", "Lisa", "Dawn", "2705551267", "Betty", "June", "5555551556", "Yvonne", "Poe", "6665558888", "Lizzy", "Loo", "5025557971", "John", "Gerald", "3335551256", "Mark", "VandenBrink", "1115557878", "Roger", "Riggs", "3335550000", "Antero", "Taivalsaari", "9995552222" }; public PopulateAddressBook() { RecordStore rs; RecordEnumeration re; CreateAddressBook.createRecordStore("TheAddressBook", personalNames); try { rs = RecordStore.openRecordStore("TheAddressBook", true); re = rs.enumerateRecords(null, null, false); while (re.hasNextElement()) { byte[] b = re.nextRecord(); System.out.println("<" + SimpleRecord.getFirstName(b) + ">" + "<" + SimpleRecord.getLastName(b) + ">" + "<" + SimpleRecord.getPhoneNum(b) + ">"); } } catch (Exception e) { System.out.println("Houston, we have a problem: " + e); } } /** * Called by the system to start our MIDlet. */ protected void startApp() { destroyApp(false); notifyDestroyed(); } /** * Called by the system to pause our MIDlet. * No actions required by our MIDLet. */ protected void pauseApp() {} /** * Called by the system to end our MIDlet. * No actions required by our MIDLet. */ protected void destroyApp(boolean unconditional) {} } /* * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. */ /** * Static helper class that creates a record * store from data in an array. */ class CreateAddressBook { // Don't allow this class to be instantiated private CreateAddressBook() {} /** * Helper method that creates a record * store from data in an array. * Returns: * true if RMS store was created * false otherwise * name the name of the record store to create * seedData an array w/ data to seed record store */ static boolean createRecordStore(String name, String[] seedData) { RecordStore recordStore; boolean ret = false; // Delete any previous record store with same name. // Silently ignore failure. try { RecordStore.deleteRecordStore(name); } catch (Exception rse) {} // Create new RMS store. If we fail, return false. try { recordStore = RecordStore.openRecordStore(name, true); } catch (RecordStoreException rse) { return ret; } ret = true; // assume success // Now, populate the record store with the seed data for (int i = 0; i < seedData.length; i += 3) { byte[] record = SimpleRecord.createRecord(seedData[i], seedData[i+1], seedData[i+2]); try { recordStore.addRecord(record, 0, record.length); } catch (RecordStoreException rse) { ret = false; break; } } // Get here when adds are complete, or an error occured. // In any case, close the record store. We shouldn't // have a failure, so silently ignore any exceptions. try { recordStore.closeRecordStore(); } catch (RecordStoreException rsne) {} return ret; } } /* * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. */ /** * This class implements the RecordComparator interface. * It works on the records created by SimpleRecord. * It sorts on either first name or last name. */ class SimpleComparator implements RecordComparator { /** * Sorting values (sort by first or last name) */ public final static int SORT_BY_FIRST_NAME = 1; public final static int SORT_BY_LAST_NAME = 2; /** * Sort order. Set by constructor. */ private int sortOrder = -1; /** * Public constructor: sets the sort order to be * used for this instantiation. * * Sanitize s: if it is not one of the * valid sort codes, set it to SORT_BY_LAST_NAME * silently. * s the desired sort order */ SimpleComparator(int s) { switch (s) { case SORT_BY_FIRST_NAME: case SORT_BY_LAST_NAME: this.sortOrder = s; break; default: this.sortOrder = SORT_BY_LAST_NAME; break; } } /** * This is the compare method. It takes two * records, and depending on the sort order * extracts and lexicographically compares the * subfields as two Strings. * * r1 First record to compare * r2 Second record to compare * return one of the following: * * RecordComparator.PRECEDES * if r1 is lexicographically less than r2 * RecordComparator.FOLLOWS * if r1 is lexicographically greater than r2 * RecordComparator.EQUIVALENT * if r1 and r2 are lexicographically equivalent */ public int compare(byte[] r1, byte[] r2) { String n1 = null; String n2 = null; // Based on sortOrder, extract the correct fields // from the record and convert them to lower case // so that we can perform a case-insensitive compare. if (sortOrder == SORT_BY_FIRST_NAME) { n1 = SimpleRecord.getFirstName(r1).toLowerCase(); n2 = SimpleRecord.getFirstName(r2).toLowerCase(); } else if (sortOrder == SORT_BY_LAST_NAME) { n1 = SimpleRecord.getLastName(r1).toLowerCase(); n2 = SimpleRecord.getLastName(r2).toLowerCase(); } int n = n1.compareTo(n2); if (n < 0) { return RecordComparator.PRECEDES; } if (n > 0) { return RecordComparator.FOLLOWS; } return RecordComparator.EQUIVALENT; } } /* * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. */ /** * This class provides static methods that allow us * to hide the format of a record. * N.B. no synchronized access is provided */ final class SimpleRecord { private final static int FIRST_NAME_INDEX = 0; private final static int LAST_NAME_INDEX = 20; private final static int FIELD_LEN = 20; private final static int PHONE_INDEX = 40; private final static int MAX_REC_LEN = 60; private static StringBuffer recBuf = new StringBuffer(MAX_REC_LEN); // Don't let anyone instantiate this class private SimpleRecord() {} // Clear internal buffer private static void clearBuf() { for (int i = 0; i < MAX_REC_LEN; i++) { recBuf.insert(i, ' '); } recBuf.setLength(MAX_REC_LEN); } /** * Takes component parts and return a record suitable * for our address book. * * return byte[] the newly created record * first record field: first name * last record field: last name * num record field: phone number */ public static byte[] createRecord(String first, String last, String num) { clearBuf(); recBuf.insert(FIRST_NAME_INDEX, first); recBuf.insert(LAST_NAME_INDEX, last); recBuf.insert(PHONE_INDEX, num); recBuf.setLength(MAX_REC_LEN); return recBuf.toString().getBytes(); } /** * Extracts the first name field from a record. * return String contains the first name field * b the record to parse */ public static String getFirstName(byte[] b) { return new String(b, FIRST_NAME_INDEX, FIELD_LEN).trim(); } /** * Extracts the last name field from a record. * return String contains the last name field * b the record to parse */ public static String getLastName(byte[] b) { return new String(b, LAST_NAME_INDEX, FIELD_LEN).trim(); } /** * Extracts the phone number field from a record. * return String contains the phone number field * b the record to parse */ public static String getPhoneNum(byte[] b) { return new String(b, PHONE_INDEX, FIELD_LEN).trim(); } }
1. | Address Book MIDlet | ||
2. | Personal Information Manager | ||
3. | Website - J2ME Enterprise Development | ![]() | |
4. | A simple Photo and Animation Album | ||
5. | Todo MIDlet |