|
|||
This example shows how to access the Palm Address Book database. In
this example, a single Address Book entry is displayed on the screen at one
time. There are three buttons on the bottom of the screen. A
next
, previous
and an exit
button.
The next
and previous
buttons walk you through
the database one at a time. A status title at the top shows what record
number you are currently looking at.
The first int is a mapping of labels to phone fields. Every 4 bits in the first int represents a mapping of each entry into the following phone label array:
static private String []phoneLabelText = {"Work", "Home", "Fax", "Other", "E-mail", "Main", "Pager", "Mobile"};You pull the five phone labels off one by one, and you also pull off the users selection of the "Display Phone" as well. The code snippet to do so is shown below:
// Options options = in.readInt(); for (i = 0; i < 5; i++) { phoneLabelId[i] = (int) (options & 0xF); options >>>= 4; } displayPhone = ((int) (options & 0xF) + 1);
A table showing the bit locations is shown below
Mapping for first phone entry label | |
Mapping for second phone entry label | |
Mapping for third phone entry label | |
Mapping for fourth phone entry label | |
Mapping for fifth phone entry label | |
Mapping for Display phone label |
The second int in the record is used to map which fields are filled in for the Address Book entry. The mapping is show below:
Last Name | |
Fist Name | |
Company | |
Phone 1 | |
Phone 2 | |
Phone 3 | |
Phone 4 | |
Phone 5 | |
Address | |
City | |
State | |
Zip Code | |
Country | |
Custom 1 | |
Custom 2 | |
Custom 3 | |
Custom 4 | |
Custom 5 | |
Note |
The screenshot at right shows the output of the program when you first
run it. If the address book is empty, a message indicating such is
displayed, otherwise the first record in the database is displayed. If
there is more than one record, the next
button is enabled,
allowing you to view the next record. The header at the top shows you
which record your are in, and how many currently exist in the database.
This screenshot shows what the output looks like if you push the
next
button at the bottom of the screen. The second record
is displayed, and the previous
button is enabled.
First, you need to be able to open the Database. I used code out of my database example, which opened up a database to read. The gotcha here is that you need to know that the typeID for the AddressBook is "DATA", and the creatorID is "addr" (remember, these are case sensitive).
When opening the database, I used a simple algorithm to convert a four character string to a byte array.
public int convertTypeToInt(String type) { byte[] b = type.getBytes(); return ((int)b[0])*(256*256*256)+ ((int)b[1])*(256*256)+ ((int)b[2])*(256)+ ((int)b[3]); }
In the constructor for the Spotlet:
readAddressRecord()
method, and display the recored in the scroll
text box by calling the toCompressedFormattedString()
method
on the AddressRecord.
next
button.
The method readAddressRecord()
is as follows:
protected void readAddressRecord(int index) { // read the record record = db.getRecord(index); // convert it into a DataInputStream byteIn = new ByteArrayInputStream(record, 0, record.length); dataIn = new DataInputStream(byteIn); // now read the data into the AddressRecord try { //translates the body of the record addressRecord.readData(dataIn); dataIn.close(); } catch (IOException ioe) { scrollText.setText("ERROR: Unable to read record " + index + "..."); } }Note the use of the ByteArray and Data InputStreams. This allows you to take an array of bytes, and use it as a standard DataInputStream. This input stream is used by the AddressRecord class to read in the values from the record.
AddressRecord is a utility class that is taken from the CKDJ4. It is found in the palm.conduit package, and contains methods to get data into and out of the DataInputStreams that have been associated with the database records. It contains methods to access the formatting bits described above that tell you how to read the entire database record correctly. The bulk of this code involves the parsing and formatting of values within an Address Book entry.