开发者

How to set record id by myself in J2ME

开发者 https://www.devze.com 2023-04-11 04:01 出处:网络
I use RecordStore to store my data. I know when we store data in RecordStore, it automatically generates a record id for each record.

I use RecordStore to store my data.

I know when we store data in RecordStore, it automatically generates a record id for each record.

But how can I set the record id by myself? Or how can I get the record id?

Because I want to use the recordstore.setRecord(..) method to update my recordstore.

But when I use RecordEnumeration to fetc开发者_如何学Ch RecordStore and use method nextRecordId(), it just shows odd or even ids. I mean when I have 8 records, it just prints out only odd or even records like

2 4 6 8

My code:

    handleRecord.openRecordStore(handleRecord.getRecordName());
    RecordEnumeration re;
    try {
         int rc = handleRecord.getRecordStore().getNumRecords();
         re = hrs.getRcs().enumerateRecords(null, null, true);

         while(re.hasNextElement()) {
             int rid = re.nextRecordId();
             System.out.println(rid);
         }
    } catch(Exception e) {
        System.out.println(e.toString());
    }


MIDP API doesn't have method to set record id by yourself.

See RecordStore API documentation for explanation how this is supposed to work.

  • "Records are uniquely identified within a given record store by their recordId, which is an integer value. This recordId is used as the primary key for the records. The first record created in a record store will have recordId equal to one (1). Each subsequent record added to a RecordStore will be assigned a recordId one greater than the record added before it. That is, if two records are added to a record store, and the first has a recordId of 'n', the next will have a recordId of 'n + 1'..."

The code that iterates the store appears OK:

     re = hrs.getRcs().enumerateRecords(null, null, true);
     while(re.hasNextElement()) {
         int rid = re.nextRecordId();
         System.out.println(rid);
     }

if you're getting only odd or even record like 2-4-6... or 1-3-5... printed as a result, first thing to check is whether you somehow removed records that are "missing" - this could be done eg using RecordStore.getVersion method:

  • "Each time a record store is modified (by addRecord, setRecord, or deleteRecord methods) its version is incremented. This can be used by MIDlets to quickly tell if anything has been modified..."
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号