Java Reference
In-Depth Information
The purpose of the application is to store one
String
per day. The user interface consists of a widget
and buttons for browsing the diary creating new diary entries.
Because RMS is available in MIDP and PDAP, we will start with the RMS-related functions for
loading and saving
String
s that can be used in both versions of the diary application. The complete
sources of both diary versions, differing in the user interface parts, are given in
Listing 5.1
for MIDP
and in
Listing 5.2
for PDAP. Here, we will focus on the RMS specific calls like opening the record
store, loading and saving
String
s, and closing the record store.
Because the RMS only provides a mechanism for handling byte array records, you need to convert the
diary entries represented as
String
s before adding them to the record store. Also, before you can
display a diary record in a text widget, you need to convert the byte array back into a
String
.
Fortunately, the functionality for these conversions is available in the
String
class. In order to
convert a
String
instance to a byte array, you can just call the
getBytes()
method of the
String
class. The following line shows how to convert a
String
to a byte array using the
getBytes()
method:
byte[] byteArray = new String ("Hello World!").getBytes();
The resulting byte array contains the
String
Hello World!.
This byte array can be stored in a record
store using the
addRecord()
or
setRecord()
method. In order to convert a byte array read from
a record store back into a
String
object, you can use the corresponding
String
constructor taking a
byte array as parameter.
Before you begin implementing the diary functionality, you need two variables containing the diary
record store and an index that points to the record currently displayed. In both versions of the
application, these variables are declared as follows:
RecordStore diary;
int currentId = 1;
Now you can start with the
loadEntry()
method that loads a diary entry from the record store and
converts it into a
String
. The
loadEntry()
method gets the ID of the desired record as input, sets
the current ID to the given value, and returns the corresponding
String
. If the ID is not valid, a new
record is created automatically, and the ID returned from
addRecord()
is set as the current ID. The
following code snippet shows the implementation of
loadEntry()
for both the MIDP and PDAP
versions:
public String loadEntry (int newId) throws RecordStoreException {
if (newId < 1 || newId > diary.getNumRecords()) {
byte [] data = "".getBytes();
currentId = diary.addRecord (data, 0, data.length);
}
else
currentId = newId;
return new String (diary.getRecord (currentId));
}
Now you can load records and also append new records, but you still need a method to update diary
entries when the user enters additional information or changes the entry. The
saveEntry()
method
stores the given string with the ID stored in the
currentId
variable:
public void saveEntry (String entry) throws RecordStoreException {
byte [] data = (entry).getBytes();
diary.setRecord (currentId, data, 0, data.length);
}
Search WWH ::
Custom Search