[英]Using RecordStore in Java J2ME
我目前正在做一些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:查找记录错误”。 我知道这是由于记录为空,但我想不出解决此问题的方法。
任何帮助将非常感激。
提前致谢。
您应该使用RecordEnumeration来访问记录:
RecordEnumeration renum = rs.enumerateRecords(null, null, false);
while (renum.hasNextElement())
{
int index = renum.nextRecordId();
if (store.getRecordSize(index) == STORE_LEN)
{
}
}
您不能依靠recordId获得任何有用的信息。 使用其他技术重新分配已删除的记录。
为了使记录真正删除,您必须关闭de RecordStore。 操作RecordStore的正确方法是打开它,使用它,最后关闭它。 希望这对您有用
您可以尝试使用以下方法:
/**
* 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.