Java Reference
In-Depth Information
addr[Contact.ADDR_COUNTRY] = country;
addr[Contact.ADDR_POSTALCODE] = street;
c.addStringArray(Contact.ADDR, Contact.ATTR_NONE, addr);
c.commit();
}
}
Most of this is boilerplate autogenerated by NetBeans; the stuff you're interested in is
invoked by the constructor: verifyPIMSupport , addContact , seed , and get_listContacts . The
constructor simply verifies support for the PIM package and invokes seed to add new
contacts to the database, creating and showing an error form if either method throws an
exception. Speaking of throwing exceptions, verifyPIMSupport does just that if it can't find
the PIM package, or if the PIM package supported by the device isn't the version that the
application expects (version 1.0).
The seed method creates two contacts by opening the PIM instance and obtaining a
ContactList to which it adds each of two statically defined contacts. This is handy code
to have around, because with it you can test your own PIM code directly in the emulator
as necessary. The addContact method uses the ContactList 's createContact method to cre-
ate an empty contact, and then adds the fields to the contact using the Contact.NAME and
Contact.ADDR fields.
The get_listContacts method is interesting, if only because it uses a separate thread
to read the contacts from the database. While you can do this synchronously, I find that
multithreading this operation leads to better behavior in the emulator; Listing 7-9 shows
the resulting ContactLoaderThread class.
Listing 7-9. Populating a List with Contacts Using the ContactLoaderThread
package com.apress.rischpater;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;
import javax.microedition.pim.*;
public class ContactLoaderThread extends Thread {
List list;
public ContactLoaderThread(List l) {
list = l;
}
 
Search WWH ::




Custom Search