在 Java J2ME 中使用 RecordStore
我目前正在做一些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:错误查找记录”。我知道这是因为记录为空,但我想不出解决这个问题的方法。
任何帮助将不胜感激。
提前致谢。
I am currently doing some J2ME development. I am having a problem in that a user can add and remove elements to the record store, and if a record gets deleted, then that record is left empty and the others don't move up one. I'm trying to come up with a loop that will check if a record has anything in it (incase it has been deleted) and if it does then I want to add the contents of that record to a list.
My code is similar to as follows:
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);
}
}
When it gets to rs.getRecordSize(i), I always get a "javax.microedition.rms.InvalidRecordIDException: error finding record". I know this is due to the record being empty but I can't think of a way to get around this problem.
Any help would be much appreciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 RecordEnumeration 来访问记录:
您不能依赖 recordId 来获得任何有用的信息。使用不同的技术重新分配已删除的记录。
You should use a RecordEnumeration to visit the records:
You can't rely on the recordId for anything useful. Use a different technique to reallocate a deleted record.
为了让您的记录真正被删除,您必须关闭RecordStore。操作 RecordStore 的正确方法是打开它,使用它,最后关闭它。希望这对你有用
In order to have your records truly deleted you have to close de RecordStore. The correct way to operate a RecordStore is by opening it, using it an finally closing it. Hope this is usefull to you
您可以尝试使用以下方法:
You could try to use the following method: