Java Micro Edition (J2ME) - Update Record using recordstore enumeration

 ̄綄美尐妖づ 提交于 2019-12-23 04:38:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!