Java Reference
In-Depth Information
Record Listeners
In contrast to the desktop file system, it is possible to register listeners to record stores. A listener is
informed whenever a record is changed in the record store it is registered to. This mechanism may be
useful if different threads or different MIDlets of the same MIDlet suite are operating on one record
store in parallel. A possible application could be some kind of automated logging service, writing
records in the background. A MIDlet providing a GUI for the service could register itself as listener to
the log record store when it is running in the foreground. Thus, it would be able to update its display of
the log entries whenever a new record is added.
The corresponding RecordListener interface consists of the following three methods:
void recordAdded (RecordStore recordStore, int id)
is called when a new record with the given ID is added to the given record store.
void recordChanged (RecordStore recordStore, int id)
is called when the given record is changed.
void recordDeleted (RecordStore recordStore, int id)
is called when the given record is deleted.
A listener implementing these methods can be registered using the addRecordListener() method
of the corresponding record store, where the only parameter is the RecordListener . You can
unregister the listener by calling the removeRecordListener() method of the record store with
the same listener as a parameter.
Storing Custom Objects
Up to this point, you have only stored byte arrays or strings in a record store. As explained in the diary
example, String s have a method with which you can convert them to byte arrays, and a constructor
that takes a byte array as input. But if you want to store your own objects, you need to provide a way to
convert them to byte arrays and back yourselves.
The simplest solution would be to add the same constructor and getBytes() method to your custom
objects that String already provides. You can implement the mapping two different ways. One
possibility is to convert the byte array to different fields in your object. The other possibility is to keep
the byte array as storage, and to implement field access methods as wrappers to portions of the byte
array. The second possibility makes sense especially when your structure consists of elements having a
fixed size. Here, we will focus on the first method, which is simpler to handle, especially for dynamic
structures.
Data Streams
Assume you are going to implement a travel list management tool, where the entries consist of the
journey destination, the date of the journey and the distance actually traveled.
Such an object could be implemented as follows:
class Journey {
 
 
Search WWH ::




Custom Search