Listing The desired search for names that begin with the substring entered by the user uses the API
import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStoreException;
import java.util.Enumeration;
import java.util.Vector; /**
This class implements the screen that enables the user to search for one or more particular records in the address book. The user enters a name or prefix that represents the name of one or more records in the address book.
public class SearchScreen extends Form implements CommandListener
private static Command go = new Command("Go", Command.SCREEN, 1); private static Command back = new Command("Back", Command.BACK, 1);
private static SearchScreen instance; private Display display;
private AddressBookMain addressBook;
private TextField keyEntry; /**
Constructor.
public SearchScreen() {
super("Search for entry"); instance = this;
PersistenceDemo pDemo = PersistenceDemo.getInstance(); display = Display.getDisplay(pDemo); addressBook = AddressBookMain.getInstance();
keyEntry = new TextField("Enter name", null, 20,
TextField.ANY);
append(keyEntry);
addCommand(go);
addCommand(back);
setCommandListener(this);
Returns the single instance of this class. Calling this method before constructing an object will return a null pointer.
- return an instance of this class.
- /
public static SearchScreen getInstance() {
return instance;
display.setCurrent(this);
Displays the data passed to it on screen. Actually this method delegates the job of displaying the data to an instance of SearchResultScreen. This method, however, sets a new instance of that class to be the current displayable.
- param results a Vector of records from the address book's record store.
- /
void displaySearchResults(Vector results) {
SearchResultScreen screen = new SearchResultScreen(results); display.setCurrent(screen);
Builds a result set of records that match a specified name. The criteria is that the record must match the name entered by the user in the "keyEntry" TextField. This method employs the
AddressBook.getMatchesByName() method to apply the specific filter that defines this name matching.
Vector buildSearchResults() {
AddressBook addressBook =
AddressBookMain.getInstance().getAddressBook();
String matchKey = keyEntry.getString(); Vector results = new Vector();
RecordEnumeration re = addressBook.getMatchesByName(matchKey); byte[] record = null;
while (re.hasNextElement()) {
record = re.nextRecord(); results.addElement(record);
catch (RecordStoreException rse) {
rse.printStackTrace();
return results;
Builds search results and displays it on the screen.
class BuildSearchResultsAction implements Runnable {
Vector results = buildSearchResults(); displaySearchResults(results);
public void commandAction(Command c, Displayable d) {
Runnable action = new BuildSearchResultsAction(); action.run();
AddressBookMain.getInstance().display();
The buildSearchResults() method in the SearchScreen class obtains an enumeration of records by calling the getMatchesByName(String matchKey) method in the AddressBook class. This method filters the records to return only those in which the name field begins with the matchKey.
The getMatchesByName() method accomplishes this filtering by passing a record filter as the first argument to the enumerateRecords() method. The instance of MatchAllNamesFilter defines the semantics of the filter, namely, to find all records that begin with the substring matchKey.
The enumerateRecords() method applies the following method of the filter object to each record in the record store:
boolean matches(byte[] candidate)
If the result is true, it includes that record in the enumeration set. Conceptually, this is similar to defining an SQL query in a relational database system. The RecordFilter object defines the search criteria.
Notice in Listing 7.2 that the RecordFilter argument was null. This is how the RecordList class can return all records in the enumeration; there is no filter to apply.
You can define multiple filters to support searching on different criteria. Following the design of Listing 7.4, you could define multiple inner classes that implement RecordFilter and use the inner class appropriate to the search at hand.
Post a comment