我有一个唱片店,里面有一个购物元素列表,我目前正在像这样序列化数据
** (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 ?
最佳答案
调用recordStore.enumerateRecords(null, null, false)
然后使用RecordEnumeration.hasNextElement()
和RecordEnumeration.nextRecord()
关于Java Micro Edition (J2ME) - 从记录存储中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2631450/