Hi John,
You just need to create a class that implements the RecordFilter interface. This is how you do it:
Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
import java.lang.*;
public class RMSexample extends MIDlet implements CommandListener {
private RecordStore recordStoreTestDb = null;
private Command exitCommand, searchCommand;
private Display display;
private Form screen;
public String str, strNumberOfRecords, strListOfRecords;
private Alert alert;
private int i, intNumberOfRecords;
private StringBuffer strbBuffer;
private RecordEnumeration newRecordEnumeration = null;
//private SearchFilter = new SearchFilter("Sample 2");
public RMSexample() {
// Get the display object for the MIDlet.
display = Display.getDisplay(this);
// Create the Exit and Search commands.
exitCommand = new Command("Exit", Command.EXIT, 2);
searchCommand = new Command("Search", Command.OK, 2);
// Create the screen form.
screen = new Form("RecordStoreSearchTest");
// Set the Exit and Add commands for the screen.
screen.addCommand(exitCommand);
screen.addCommand(searchCommand);
screen.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException {
// Open the record store.
try {
recordStoreTestDb = RecordStore.openRecordStore("recordstoretestfile", true);
} catch (RecordStoreNotFoundException rsnfe) {
// Handle the exception.
} catch (RecordStoreFullException fsfe) {
// Handle the exception.
} catch (RecordStoreException rse) {
// Handle the exception.
}
// If the record store has no records yet, write to it.
try {
intNumberOfRecords = recordStoreTestDb.getNumRecords();
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
if (intNumberOfRecords == 0) {
writeRecordToRecordStore("Testing 1");
writeRecordToRecordStore("Sample 2");
writeRecordToRecordStore("Sample 3");
}
// Read from and perform a search filter on the record store.
String strTheRecord = null;
try{
if (recordStoreTestDb.getNumRecords() > 0) {
RF searchFilter=new RF("Sample 2");
RecordEnumeration reTestEnumeration=recordStoreTestDb.enumerateRecords(searchFilter,null,false);
try {
strTheRecord = new String(reTestEnumeration.nextRecord());
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
System.out.println("------------------------------");
System.out.println(strTheRecord);
System.out.println("------------------------------");
/*
while (reTestEnumeration.hasNextElement()) {
try {
System.out.println(searchFilter.matches("Sample2".getBytes()));
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("------------------------------");
System.out.println(strTheRecord);
System.out.println("------------------------------");
}*/
}
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
// Close the default receipt categories record store.
try {
recordStoreTestDb.closeRecordStore();
}
catch (Exception error) {
alert = new Alert("Error Closing",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
public void writeRecordToRecordStore(String strRecord) {
byte[] bytesRecord = strRecord.getBytes();
try {
recordStoreTestDb.addRecord(bytesRecord, 0, bytesRecord.length);
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
}
/*
class SearchFilter implements RecordFilter {
public boolean matches(byte[] candidate) {
String c = new String(candidate);
if (c.equals("Sample 2")) {
return true;
} else {
return false;
}
}
}
*/
class SearchFilter implements RecordFilter {
private String searchText = null;
public SearchFilter(String searchText) {
// Convert the search text to all lowercase letters.
this.searchText = searchText.toLowerCase();
}
public boolean matches(byte[] candidate) {
// Convert the candidate text from the record store to all lowercase letters.
String str = new String(candidate).toLowerCase();
// Determine whether the search text and the candidate text from the record store match.
if (searchText != null && str.indexOf(searchText) != -1)
return true;
else
return false;
}
}
Then you add this class:
Code:
import javax.microedition.rms.RecordFilter;
public class RF implements RecordFilter {
private String match;
public RF(String match)
{
this.match=match;
}
public boolean matches(byte[] candidate) {
// TODO Auto-generated method stub
String s=new String(candidate);
if (s.equals(match))
return true;
else
return false;
}
}