问题
I have a record store of items which have (name, quantity, owner, status)
Now when the user triggers an event I want to set the status of all items in my RecordStore with "purchased"
re = shoppingListStore.enumerateRecords(null, null, false);
while (re.hasNextElement())
{
// read current values of item
byte [] itemRecord = re.nextRecord();
// deserialise byte array
newItemObject.fromByteArray(itemRecord);
// set item status to purchased
newItemObject.setItemStatus("Purchased");
// create new bytearray and call newitemobject . tobytearray
// method to return a byte array of the objects
// (using UTF8 encoded strings~)
byte[] itemData = newItemObject.toByteArray();
// add new byte array to shoppinglist store
shoppingListStore.setRecord(re.nextRecordId(), itemData, 0, itemData.length);
}
However I am overwriting the next record (using the nextRecordId). I've tried using nextRecordId - 1
but obviously this is out of bounds on the first one
Hope you can help?
回答1:
Have you tried that ?
re = shoppingListStore.enumerateRecords(null, null, false);
while (re.hasNextElement())
{
int id = re.nextRecordId();
// read current values of item
byte [] itemRecord = shoppingListStore.getRecord(id);
// deserialise byte array
newItemObject.fromByteArray(itemRecord);
// set item status to purchased
newItemObject.setItemStatus("Purchased");
// create new bytearray and call newitemobject . tobytearray method to return a byte array of the object (using UTF8 encoded strings~)
byte[] itemData = newItemObject.toByteArray();
// update shoppinglist store record with new byte array
shoppingListStore.setRecord(id, itemData, 0, itemData.length);
}
来源:https://stackoverflow.com/questions/2632788/java-micro-edition-j2me-update-record-using-recordstore-enumeration