的Java微型版(J2ME) - 更新记录使用的RecordStore枚举
题
我有有(名称,数量,所有者,状态)项的记录存储
现在,当用户触发我想与“购买”
设置我的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
尝试,但显然这是出界上的第一个
希望你能帮忙吗?
解决方案
你试过了吗?
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);
}
不隶属于 StackOverflow