tags:

views:

142

answers:

1

I have this code and for some reason I can't get it to work?

Can anyone see the problem?

I need this data to be able to return all the records that contain the same name that is entered in the mNameSearchDelete textbox. however no records are returned which should be there.

thank you

protected void searchForNameToDelete() {
    carryOutNameSearch(mNameSearchDelete, mListFormDelete);
}
private void carryOutNameSearch(TextBox theInputScreen, Form theOutputForm) {
   listOfIDs = new Vector();  // save IDs of records in case we want to delete
   theOutputForm.deleteAll(); // clear the form
    try {
        RecordStore rs = RecordStore.openRecordStore("EventsDatabase", true);

        // Use the inner class so that the enumeration only gives
        // us those records with a matching name.
        RecordEnumeration re = rs.enumerateRecords(new NameMatcher(theInputScreen.getString()), null, false);
        while (re.hasNextElement()) {
            int id = re.nextRecordId();
            listOfIDs.addElement(new Integer(id));
            byte [] recordBuffer = rs.getRecord(id);
            String record = new String(recordBuffer);

            // extract the name and the age from the record

            int endOfnameEvent = record.indexOf(";");
            int endOfdescEvent = record.indexOf(";", endOfnameEvent + 1);

            String name = record.substring(0, endOfnameEvent);
            String desc = record.substring(endOfnameEvent + 1, endOfdescEvent);
            theOutputForm.append(name + " description: " + desc + "\n");
        }
        rs.closeRecordStore();
    }
    catch(Exception e){
       // mAlertConfirmDetailsSaved.setString("Couldn't read details");
        System.err.println("Error accessing database");
    }
    Display.setCurrent(theOutputForm);

}

  /* An inner class to allow us to select only
*  those records with a matching name.
*/
static class NameMatcher  implements RecordFilter {
    String nameToMatch;
    public NameMatcher(String nameEvent) {
        nameToMatch = nameEvent;
    }
    public boolean matches(byte[] record) {
        if (record.length < nameToMatch.length()) {
            return false;
        }
        String strRecord = new String(record);
        if (strRecord.startsWith(nameToMatch)) {
            return true;
        }
        return false;
    }
}
+2  A: 

I can see one thing that is wrong:

if (record.length < nameToMatch.length()) {
    return false;
}

you are comparing a number of bytes with a number of characters.

Maurice Perry
Not only that, != is more correct than < in this context.
Ian Kemp