Java Micro Edition (J2ME) - 从记录存储中删除元素
我有一个记录存储,其中包含购物商品列表,我目前正在序列化数据,如下所示
** (inside) ItemClass **
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);
dataOutputStream.writeUTF(getOwner());
dataOutputStream.writeUTF(getItemName());
dataOutputStream.writeInt(getQuantity());
dataOutputStream.close();
return byteOutputStream.toByteArray();
然后将字节数组写入记录存储,如下所示(对每个 id 使用 for 循环)
for (int i = 1; i <= shoppingListStore.getNumRecords(); i++)
{
byte[] itemRecord = shoppingListStore.getRecord(i);
newItemObject.fromByteArray(itemRecord);
userShoppingList.append(newItemObject.getItemName() + " " + newItemObject.getQuantity() + " " + newItemObject.getOwner(), null);
}
问题来自于使用 ID 来遍历记录存储,因为这可能会因删除等而改变,因此我不断收到无效的 id 被抛出。我知道我不能使用 For every ItemObj : Recordstore 那么我如何遍历记录存储而不必担心 ids ?
I have a record store which holds a list of shopping items i am currently serialising the data like so
** (inside) ItemClass **
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);
dataOutputStream.writeUTF(getOwner());
dataOutputStream.writeUTF(getItemName());
dataOutputStream.writeInt(getQuantity());
dataOutputStream.close();
return byteOutputStream.toByteArray();
This byte array is then written to the record store like so (using for loop for each id)
for (int i = 1; i <= shoppingListStore.getNumRecords(); i++)
{
byte[] itemRecord = shoppingListStore.getRecord(i);
newItemObject.fromByteArray(itemRecord);
userShoppingList.append(newItemObject.getItemName() + " " + newItemObject.getQuantity() + " " + newItemObject.getOwner(), null);
}
The problem comes from using the ID to treverse through the record store as this may change from deletion etc and as such i keep geting invalid id being thrown. I know i cannot use For each ItemObj : Recordstore so how do i treverse through the record store without having to worry about ids ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
recordStore.enumerateRecords(null, null, false)
然后使用
RecordEnumeration.hasNextElement()
和RecordEnumeration.nextRecord()
Call
recordStore.enumerateRecords(null, null, false)
Then use
RecordEnumeration.hasNextElement()
andRecordEnumeration.nextRecord()