session10 j2me record management system

9
Outline-session 9 (09-April- 2009) >> J2ME Record Management System -Notification of Changes with Record Listener -Exception Handling

Upload: muthusvm

Post on 26-May-2015

1.448 views

Category:

Technology


1 download

DESCRIPTION

J2ME Record Management System

TRANSCRIPT

Page 1: Session10 J2ME Record Management System

Outline-session 9 (09-April-2009)

>> J2ME Record Management System-Notification of Changes with Record Listener-Exception Handling

Page 2: Session10 J2ME Record Management System

Sorting with Record Comparator• RecordComparator is a Java interface• implement this interface when you would like the

enumerator to return records in sorted orderImplementation:public class Comparator implements RecordComparator{public int compare(byte[] rec1, byte[] rec2){String str1 = new String(rec1), str2 = new String(rec2);int result = str1.compareTo(str2);if (result == 0)return RecordComparator.EQUIVALENT;else if (result < 0)return RecordComparator.PRECEDES;elsereturn RecordComparator.FOLLOWS;}}

Page 3: Session10 J2ME Record Management System

Sorting with Record Comparator

// Create a new comparator for sortingComparator comp = new Comparator();// Reference the comparator when creating the result setRecordEnumeration re = rs.enumerateRecords(null,comp,false);// Iterate through the sorted resultswhile (re.hasNextElement()){String str = new String(re.nextRecord());...}

Page 4: Session10 J2ME Record Management System

Record Comparator API

Page 5: Session10 J2ME Record Management System

String Sort with Compound Records

• When a record has multiple data types stored within it (what I'll refer to as a compound record), there may be more than one field that would be appropriate for sorting

String[] pets = {"duke", "tiger", "spike", "beauregard"};boolean[] dog = {true, false, true, true};

int[] rank = {3, 0, 1, 2};• StringSort.java• IntSort.java

Page 6: Session10 J2ME Record Management System

Searching with RecordFilter

• an enumerator can filter records• When using RecordComparator all records in a record store

are in the result set. With a RecordFilter, only those records that match the filter criteria will become part of the enumerator result set.

Page 7: Session10 J2ME Record Management System

Searching with RecordFilterclass SearchFilter implements RecordFilter{private String searchText = null;public SearchFilter(String searchText)304{// This is the text to search forthis.searchText = searchText.toLowerCase();}public boolean matches(byte[] candidate){String str = new String(candidate).toLowerCase();// Look for a matchif (searchText != null && str.indexOf(searchText) != -1)return true;elsereturn false;}}

Page 8: Session10 J2ME Record Management System

Searching with RecordFilter

// Create a new search filterSearchFilter search = new SearchFilter("search text");// Reference the filter when creating the result setRecordEnumeration re =rs.enumerateRecords(search,null,false);// If there is at least one record in result set, a matchwas foundif (re.numRecords() > 0)// Do something

Page 9: Session10 J2ME Record Management System

RecordFilter -API

• RecordFilter API• Table 11.6. RecordFilter Interface:

javax.microedition.rms.RecordFilter• Method Description• boolean matches(byte[] candidate) Search a

record for a specific value