我目前正在做一些J2ME开发。我遇到了一个问题,因为用户可以在记录存储中添加和删除元素,如果一条记录被删除,那么该记录将保留为空,而其他记录不会上移一条。我正在尝试一个循环,它将检查一条记录中是否有任何内容(以防它已被删除),如果有,我想将该记录的内容添加到一个列表中。我的代码类似于以下代码:
for (int i = 1; i <= rs.getNumRecords(); i++)
{
// Re-allocate if necessary
if (rs.getRecordSize(i) > recData.length)
recData = new byte[rs.getRecordSize(i)];
len = rs.getRecord(i, recData, 0);
st = new String(recData, 0, len);
System.out.println("Record #" + i + ": " + new String(recData, 0, len));
System.out.println("------------------------------");
if(st != null)
{
list.insert(i-1, st, null);
}
}当它到达rs.getRecordSize(i)时,我总是得到一个"javax.microedition.rms.InvalidRecordIDException: error finding record“。我知道这是因为记录是空的,但我想不出解决这个问题的方法。
任何帮助都将不胜感激。
提前谢谢。
发布于 2010-03-23 00:02:45
您应该使用RecordEnumeration来访问记录:
RecordEnumeration renum = rs.enumerateRecords(null, null, false);
while (renum.hasNextElement())
{
int index = renum.nextRecordId();
if (store.getRecordSize(index) == STORE_LEN)
{
}
}你不能依赖recordId来做任何有用的事情。使用不同的技术重新分配已删除的记录。
发布于 2010-04-06 20:43:37
为了让你的记录真正被删除,你必须关闭de RecordStore。操作RecordStore的正确方法是打开它,使用它,最后关闭它。希望这对你有用
发布于 2010-03-28 20:47:05
您可以尝试使用以下方法:
/**
* checks if record i is a valid records
* @param i (recordId
* @return true/false
*/
public boolean isValidRecord(int id) {
try {
recordStore.getRecordSize(id);
return true;
} catch (RecordStoreNotOpenException e1) {
e1.printStackTrace();
return false;
} catch (InvalidRecordIDException e1) {
//e1.printStackTrace(); //this printStackTrace is hidden on purpose: the error is in fact used to find out if the record id is valid or not.
return false;
} catch (RecordStoreException e1) {
e1.printStackTrace();
return false;
}
}https://stackoverflow.com/questions/2493648
复制相似问题