Java Reference
In-Depth Information
} else {
byte b[] = l.toBytes();
openStore();
store.addRecord(b, 0, b.length);
closeStore();
}
}
public void updateLocation(Location l) throws RecordStoreException {
int id = l.getId();
// If it has an id, do an update on that id.
if (id != Location.NO_ID) {
byte b[]=l.toBytes();
openStore();
store.setRecord(id,b,0,b.length);
} else {
// If it doesn't have an id, find it.
Location target = getLocation(l.getLocation());
// If there is a record matching this one, update it
if (target!=null) {
target.setForecast(l.getForecast());
updateLocation(target);
} else {
// otherwise add this one.
addLocation(l);
}
}
closeStore();
}
}
The private methods openStore and closeStore get called a lot. I considered moving
openStore to the constructor and adding a required cleanup method that the client must
call when releasing the interface (remember, the MIDP doesn't support object finaliza-
tion!), but I decided that explicit finalization would be a burden on the clients of the
class. This is subject to change; testing on devices with a lot of locations might dictate a
different design. Anyway, these methods are fairly simple: openStore opens the store if it
isn't already open, rethrowing any exceptions in the event of an error; closeStore closes
the store, concealing any errors that might occur.
The getLocationStrings method returns an array of Location items; you can use
it for populating the List of locations in the main user interface. It uses a simple
RecordEnumeration , relying on the enumeration to order the resulting enumeration in
 
Search WWH ::




Custom Search