Namespaces
Variants
Actions

Record sorting in RMS

Jump to: navigation, search
Article Metadata

Article
Created: sparkystar (20 Dec 2007)
Last edited: hamishwillee (11 Jul 2012)

Sorting


RecordEnumeration is a class that allows forward and backward movement through a record store. Moving through the store requires nothing more than a check to see if there is another record available (in either direction), and if so, requesting its data.

RecordStore rs;
...
RecordEnumeration re = rs.enumerateRecords(null, null, false);
while (re.hasNextElement())
{
String str = new String(re.nextRecord());
System.out.println("Record: " + str);
}

RecordComparator is a Java interface. You implement this interface when you would like the enumeration to return records in sorted order. There is only one method in the RecordComparator interface, compare(). This method is called by the enumeration to create the (sorted) result set. Here is a simple class that implements the interface.

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;
else
return RecordComparator.FOLLOWS;
}
}

When exiting the compare() method, you must return one of the three pre-defined return values. The following code block shows how you might use the Comparator class with an enumeration.

RecordStore rs;
...
// Create a new comparator for sorting
Comparator comp = new Comparator();
 
// Reference the comparator when creating the result set
RecordEnumeration re = rs.enumerateRecords(null, comp, false);
 
// Move through the sorted results
while (re.hasNextElement())
{
String str = new String(re.nextRecord());
...
}

Complete code example

// The following example demonstrates how to sort the records in RMS using RecordComparator
 
 
// Record Comparator ......
 
 
 
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
 
public class RMSSort extends MIDlet implements CommandListener {
private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command start;
private Command delete;
private Command show;
 
 
private RecordStore recordstore = null;
private RecordEnumeration recEnum;
 
public RMSSort() {
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.SCREEN, 1);
show = new Command("Show",Command.SCREEN,2);
delete = new Command("Delete",Command.SCREEN,2);
form = new Form("RMS Sort");
form.addCommand(exit);
form.addCommand(start);
form.addCommand(show);
form.addCommand(delete);
 
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp( boolean unconditional )
{
}
 
public void commandAction(Command command, Displayable d) {
if (command == start)
{
try
{
recordstore = RecordStore.openRecordStore("RMSSORT",true);
String[] names= {"senthil", "kumar","sri","dhana","arun"};
for(int i=1;i<names.length;i++){
byte[] outputData = names[i].getBytes();
recordstore.addRecord(outputData,0,outputData.length);
}
int numRec = recordstore.getNumRecords();
System.out.println(" no of rec "+numRec);
}catch(Exception e){System.out.println("Exp"+e);}
}
else if (command == show)
{
StringBuffer recBuffer = new StringBuffer();
Comparator comp = new Comparator();
try{
int numRec = recordstore.getNumRecords();
recEnum = recordstore.enumerateRecords(null,comp,false);
while(recEnum.hasNextElement())
{
recBuffer.append( new String(recEnum.nextRecord()));
recBuffer.append("\n");
}
alert = new Alert(" Records ",recBuffer.toString(),null, AlertType. WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}catch(Exception e){System.out.println("Exp"+e);}
}
else if(command == delete)
{
try{
if(recordstore != null){
int numRec = recordstore.getNumRecords();
for(int i=1;i<=numRec;i++)
recordstore.deleteRecord(i);
// System.out.println(" no of rec "+numRec);
}
}catch(Exception e){
System.out.println(" Exception in delete Rec:" +e);
}
 
}
 
 
}
 
class Comparator implements RecordComparator
{
public Comparator()
{
}
public int compare(byte[] rec1 , byte[] rec2)
{
 
String s1 = new String(rec1);
String s2 = new String(rec2);
int comp = s1.compareTo(s2);
if(comp==0)
{
return RecordComparator.EQUIVALENT;
}
else if(comp <0)
{
return RecordComparator.PRECEDES;
}
else
{
return RecordComparator.FOLLOWS;
}
}
}
} // End of main class
This page was last modified on 11 July 2012, at 08:19.
110 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved