This article will mainly describe the usage of Recordfilter and Recordenumeration, which is critical in the entire Record Management System. Since I feel that RecordComparator and Recordfilter are similar and have been relatively small, they will not be introduced here. We are still how to use these two interfaces through a demonstration application.
Recordfilter's definition is very simple, he only defines a method that is Boolean Matches (Byte [] Data). Public interface recordfilter {boolean matches (Byte [] Recorddata);} It is also very simple to use, we only need to implement this method and return to the value of the Boolean type as needed, usually we use this interface when looking up the record, It is passed as a parameter to the EnumerateRecords () method, such as Recordenumeration Records = RS.EnumeRerecords (New RecordryFilter (Key), Null, False), first we look at the parameters of this method, the first parameter is Recordfilter, it is used Filtering the record in the database is the method we defined above. Boolean matches (byte [] data), the second parameter is RecordComparator, which is sorted for the selected data, if you don't specify Sort by default order. The third parameter is a boolean type. If it is true, then Record will track data change in RMS, which is more expensive, I usually use false. After obtaining records we can make a lot of useful operations, the specific method is: public interface RecordEnumeration {void destroy (); boolean hasNextElement (); boolean hasPreviousElement (); boolean isKeptUpdated (); void keepUpdated (boolean keepUpdated); byte [] nextRecord () throws InvalidRecordIDException, RecordStoreNotOpenException, RecordStoreException; int nextRecordId () throws InvalidRecordIDException; int numRecords (); byte [] previousRecord () throws InvalidRecordIDException, RecordStoreNotOpenException, RecordStoreException; int previousRecordId () throws InvalidRecordIDException; void rebuild (); void reset ( }
The method of marking is often used in mind. When we implement Recordfilter, it is usually written into an internal class of a class, which is very reasonable. My procedure below still uses this method, Private Static Class Recordryfilter Implements Recordfilter {Private String Key;
public RecordEntryFilter (String key) {this.key = key;} public boolean matches (byte [] data) {try {return RecordEntry.matches (data, key);} catch (IOException e) {e.printStackTrace (); return False;}}} In an example, we store several data entities to RMS, which includes a UserName and a phonenumber field. When we write and read the fields, you can use the serialization mechanisms in the series. Import java.io. *;
Public class recording {private string username; private string phoneenen;
Public Recordentry () {}
Public Recordentry (String Username, String Phonenum) {this.usename = Username; this.phonenum = phonenum;
Public string getphonenum () {return phoneenum;}
Public void setphoneenum (string phoneenum) {this.phonenum = phonenum;
Public string getUsername () {return username;}
Public void setusername (string username) {this.username = usrname;}
Public Byte [] Serialize () throws ioException {byteArrayoutputstream baos = new byterrayoutputstream (); DataOutputStream dos = New DataputPutStream (baos);
dos.writeutf (username); dos.writeutf (phonenum);
Return baos.tobytearray ();
public static RecordEntry deserialize (byte [] data) throws IOException {ByteArrayInputStream bais = new ByteArrayInputStream (data); DataInputStream dis = new DataInputStream (bais); RecordEntry record = new RecordEntry (); record.userName = dis.readUTF (); record .phoneNum = dis.readUTF (); return record;} public static boolean matches (byte [] data, String key) throws IOException {ByteArrayInputStream bais = new ByteArrayInputStream (data); DataInputStream dis = new DataInputStream (bais); return dis. Readutf (). Equals (key);} public string toString () {Return UserName : " phonenum;}} The other focus on our this article is how to use Recordenumeration, the relevant code is written in a RecordModel class as follows : Import java.io.ioException;
Import javax.microedition.rms. *;
public class RecordModel {private RecordStore rs; private boolean firstTime = true; public static final String NAME = "record"; private RecordEntry [] record = {new RecordEntry ( "ming", "12345"), new RecordEntry ( "pain", "123456"), New Recordentry ("Linux", "234566"), New Recordentry ("Mingtian", "3456677")}
Private static class recordingfilter imports recordfilter {private string key;
Public RecordiffEnTryfilter (String Key) {this.key =
Public boolean matches (byte [] data) {Try {return recording;} catch (ioException e) {E.PrintStackTrace (); return false;}} RecordStore.OpenRecordStore (name, true);} catch (recordstoreException e) {E.PrintStackTrace ();} if (firsttime) {init (); firsttime = false;}}
Public void init () {try = 0; I }}} Catch (ooException e) {E.PrintStackTrace ();} catch (recordstoreException E) {E.PrintStackTrace ();} } public RecordEntry [] getRecord (String key) {try {if (rs.getNumRecords ()> 0) {RecordEnumeration records = rs.enumerateRecords (new RecordEntryFilter (key), null, false); int length = records.numRecords (); IF (length == 0) {return new recording [0];} else {system.out.println (length); recording = new recording [longth]; for (int i = 0; i protected void startApp () throws MIDletStateChangeException {display = Display.getDisplay (this); mainForm = new Form ( "Test"); model = new RecordModel (); textField = new TextField ( "input:", null, 20, TextField. Any); Mainform.Append (TextField); Mainform.SetItemStatelistener (this); Display.SetCurrent (Mainform); protected void pauseapp () {} Protected Void DestroyApp (Boolean Arg0) throws MidletStateChangeException {} public void itemStateChanged (Item item) {if (item == textField) {String input = textField.getString (); RecordEntry [] record = model.getRecord (input); if (record.length == 0) {System.out .println ("no record");} else { For (int i = 0; i } For convenience I have not displayed on the mobile interface, but output to the console. Here is the result of printing in the console during the Input: No Recordno Recordno Record1Ming: 12345 You may want to filter the recorded records starting with the input field, just like queries in your address book. Then you can add an int type Type to the Filter to tell how the Matches () method in the entity should do it. This will filter out the record you need :) Or that sentence, I hope to use it! I originally wanted to introduce my own personal communication in the series 5, the program has been written, about 3K code, basically cover the topic described above. Since I prepare for submission, I decided not to continue to write the contents of Record Management System.