Java Micro Edition (J2ME) - обновление записи с использованием перечисления хранилища записей
У меня есть магазин записей товаров, которые имеют (имя, количество, владелец, статус)
Теперь, когда пользователь запускает событие, я хочу установить статус всех предметов в моем RecordStore с помощью "куплено"
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);
}
Однако я перезаписываю следующую запись (используя nextRecordId). Я пытался использовать nextRecordId - 1
но очевидно, что это за пределами первого
Надеюсь, вы можете помочь?
1 ответ
Решение
Вы пробовали это?
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);
}