2010-04-13 50 views
0

我有有项目(名称,数量,所有者,状态)的唱片店的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,但显然这是第一个出界

希望你能帮忙吗?

回答

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

刚试过上面的代码,如果你ammend的re.getRecord(ID)和做recordStoreName.getRecord(ID),那么它的工作原理治疗:)谢谢(编辑你的代码,我会剔你的答案)谢谢 – Garbit 2010-04-13 20:23:57

+0

对,我更新了帖子 – 2010-04-13 20:49:48