×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Sep 2011
    Posts
    73

    Need help filtering a record store.

    I'm trying to filter a record store, but thus far I have been unsuccessful. Three records are written to the record store:

    Testing 1
    Sample 2
    Sample 3

    I then try to filter using "Sample 2". For the class SearchFilter at the bottom of the below code, I am not sure where and with what exact code to specify that the text to be filtered with is "Sample 2".

    I had also tried filtering using some different code for the SearchFilter class. That code has been commented out.

    There is a while loop that has been commented out also.

    Below is the code:

    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 RecordStoreSearchTest 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 searchFilter = null;
    	private SearchFilter = new SearchFilter("Sample 2");
    
    	public RecordStoreSearchTest() {
    		// 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) {
    				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 {
    						strTheRecord = new String(reTestEnumeration.nextRecord());
    					} catch (RecordStoreException 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;
    	}
    }
    Might anyone know what needs to be done to cause the filtering to work?

  2. #2
    Nokia Developer Expert
    Join Date
    Aug 2011
    Posts
    555

    Re: Need help filtering a record store.

    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;
    	}
    
    }

  3. #3
    Regular Contributor
    Join Date
    Mar 2011
    Location
    Pune,Maharashtra,India
    Posts
    92

    Re: Need help filtering a record store.

    Quote Originally Posted by John_123 View Post
    I'm trying to filter a record store, but thus far I have been unsuccessful. Three records are written to the record store:

    Testing 1
    Sample 2
    Sample 3

    I then try to filter using "Sample 2". For the class SearchFilter at the bottom of the below code, I am not sure where and with what exact code to specify that the text to be filtered with is "Sample 2".

    I had also tried filtering using some different code for the SearchFilter class. That code has been commented out.

    There is a while loop that has been commented out also.

    Below is the code:

    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 RecordStoreSearchTest 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 searchFilter = null;
    	private SearchFilter = new SearchFilter("Sample 2");
    
    	public RecordStoreSearchTest() {
    		// 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) {
    				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 {
    						strTheRecord = new String(reTestEnumeration.nextRecord());
    					} catch (RecordStoreException 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;
    	}
    }
    Might anyone know what needs to be done to cause the filtering to work?
    Check the line number 20,As you have not declared the object of SearchFilter class.here you only wrote the class name.
    Regards,
    Pradip

  4. #4
    Regular Contributor
    Join Date
    Sep 2011
    Posts
    73

    Re: Need help filtering a record store.

    Thank you, skalogir and pradip7. It works now. The new code is listed below. In order to test for the matching of strings that differ only insofar as capitalization is concerned, the search filter is specified as "saMple 2" to see if it would match "Sample 2".

    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 RecordStoreSearchTest 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 searchFilter = null;
    	//private RecordFilter SearchFilter = new SearchFilter("Sample 2");
    
    	public RecordStoreSearchTest() {
    		// 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) {
    				//RecordFilter SearchFilter = new SearchFilter("Sample 2");
    				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 {
    						//strTheRecord = new String(reTestEnumeration.nextRecord());
    						System.out.println(searchFilter.matches("Sample2".getBytes()));
    					//} catch (RecordStoreException ex) {
    					} 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;
    	}
    }
    
    //public class RF implements RecordFilter {
    class RF implements RecordFilter {
    
    	private String match;
    	
    	public RF(String match)
    	{
    		//this.match=match;
    		this.match=match.toLowerCase();
    	}
    	
    	public boolean matches(byte[] candidate) {
    		// TODO Auto-generated method stub
    		String s=new String(candidate);
    		//if (s.equals(match))
    		if (s.toLowerCase().equals(match))
    		//if (s.startsWith(match))
    			return true;
    		else
    			return false;
    	}
    
    }

Similar Threads

  1. Appending another byte array to the same record in a record store
    By cryptyritu in forum Mobile Java General
    Replies: 2
    Last Post: 2011-01-13, 07:48
  2. Store string in Record Store
    By Nourayn in forum Mobile Java General
    Replies: 15
    Last Post: 2010-08-21, 04:08
  3. Record store
    By nishaks in forum Mobile Java Tools & SDKs
    Replies: 3
    Last Post: 2010-02-19, 06:17
  4. Encrypting data to store in record store
    By matan_d in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2009-01-04, 01:21
  5. Record Store vs JDBC. How to store data?
    By 180ºrevolution in forum Mobile Java General
    Replies: 0
    Last Post: 2004-09-17, 15:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×