Java Reference
In-Depth Information
public void startApp() {
Display.getDisplay(this).setCurrent(mForm);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {
// Save the user name and password.
mPreferences.put(kUser, mUserField.getString());
mPreferences.put(kPassword, mPasswordField.getString());
try { mPreferences.save(); }
catch (RecordStoreException rse) {}
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT) {
destroyApp(true);
notifyDestroyed();
}
}
}
All the RecordStore work is encapsulated in the Preferences class shown in Listing 8-2.
Preferences is a wrapper for a map of string keys and values, stored internally as mHashtable .
When a Preferences object is created, key and value pairs are loaded from the record store.
A key and value pair is stored in a single record using a pipe character separator (|).
Preferences uses a RecordEnumeration to walk through all the records in the record store.
We'll talk about RecordEnumeration soon; for now, just trust us when we tell you it gives you a
way to paw through the data in the record store.
Listing 8-2. A Class That Encapsulates RecordStore Access
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class Preferences {
private String mRecordStoreName;
private Hashtable mHashtable;
public Preferences(String recordStoreName)
throws RecordStoreException {
mRecordStoreName = recordStoreName;
mHashtable = new Hashtable();
load();
}
Search WWH ::




Custom Search