0

我有一个记录商店,其中包含(名称、数量、所有者、状态)

现在,当用户触发事件时,我想将 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,但显然这超出了第一个的范围

希望你能帮忙?

4

1 回答 1

3

你试过吗?

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);
}
于 2010-04-13T20:16:49.440 回答