本文介绍了Java Micro Edition(J2ME)-使用记录存储枚举更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个记录存储项,其中包含(名称,数量,所有者,状态)

I have a record store of items which have (name, quantity, owner, status)

现在,当用户触发事件时,我想使用已购买"设置RecordStore中所有项目的状态

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

但是,我正在覆盖下一个记录(使用nextRecordId).我已经尝试过使用nextRecordId - 1,但是显然这在第一个范围内是不可行的

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

希望您能提供帮助吗?

推荐答案

您尝试过吗?

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

这篇关于Java Micro Edition(J2ME)-使用记录存储枚举更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 00:33