【发布时间】:2009-03-20 14:28:23
【问题描述】:
我有这个代码,但由于某种原因我无法让它工作?
谁能看出问题所在?
我需要此数据才能返回包含在 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;
}
}
【问题讨论】:
-
也许您应该考虑描述一下究竟是什么不起作用。我们不介意读者。
-
告诉我们它是做什么的,然后告诉我们它做了什么是错的。如果你连这件事都懒得做,那你就不值得帮助,IMNSHO。
-
我同意,请输入您的代码不适用的内容。
-
完全同意(并且反对)
-
对不起,我需要它来显示数据库中与输入的数据相同的记录。当我知道记录在那里时,它会显示一个空白表格。如果你能帮助我会appeciate