Java Micro Edition (J2ME) - Actualizar registro utilizando la enumeración RecordStore

StackOverflow https://stackoverflow.com/questions/2632788

  •  26-09-2019
  •  | 
  •  

Pregunta

Tengo una tienda de discos de artículos que tienen (nombre, cantidad, propietario, estado)

Ahora, cuando el usuario activa un evento que quiero establecer el estado de todos los artículos en mi RecordStore con "comprar"

        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);
        }

Sin embargo, estoy sobrescribiendo el siguiente registro (utilizando el nextRecordId). He intentado usar nextRecordId - 1 pero obviamente esto está fuera de límites en el primero

La esperanza puede ayudar?

¿Fue útil?

Solución

¿Has probado esto?

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);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top