我有这个代码,由于某种原因我不能让它工作?
有人能看到问题吗?
我需要此数据才能返回包含在mNameSearchDelete文本框中输入的相同名称的所有记录。但是没有返回应该存在的记录。
谢谢
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;
}
}
答案 0 :(得分:2)
我可以看到一件事是错的:
if (record.length < nameToMatch.length()) {
return false;
}
您正在将多个字节与多个字符进行比较。