Вопрос

I'm trying to "wrap" the javax.microedition.lcdui.List class into MyList class, I just want to use my own append, edit, delete methods (also getSelectedFlags but from superclass).

I also want to manage the recordStore things, but I found that the indexing in List is strange for me, all element's index decreases when I delete first element of the list etc (not sure what it really is doing).

I found solution where I save things into vector, which has got similar behavior to List, but I don't want to save things in a vector and also in recordStore..

package hello;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class ListOfPersons extends List {

    RecordStore rs;
    Hashtable hash;

    public ListOfPersons(String title) {
        super(title, List.IMPLICIT);
        hash = new Hashtable();
        this.loadAllRecords();
    }

    public void delete(int elementNum) {
        Integer rsID = (Integer) hash.get(new Integer(elementNum));
        openRS();
        try {
            rs.deleteRecord(rsID.intValue());
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        closeRS();

        hash.remove(new Integer(elementNum));
        super.delete(elementNum);
    }

    public Person getSelectedPerson() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public int append(Person element) {
        Integer idList, idRecord = null;
        openRS();
        idList = new Integer(super.append(element.toString(), null));

        byte[] b;
        try {
            b = element.storeToBytes();
            idRecord = new Integer(rs.addRecord(b, 0, b.length));
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            closeRS();
        }
        hash.put(idList, idRecord);
        return idList.intValue();
    }

    private void loadAllRecords() {
        Integer idList, idRecord;
        Person p;
        openRS();
        try {
            RecordEnumeration re = rs.enumerateRecords(null, null, true);
            while (re.hasNextElement()) {
                p = new Person();
                idRecord = new Integer(re.nextRecordId());
                byte[] b = rs.getRecord(idRecord.intValue());
                p.loadFromBytes(b);
                idList = new Integer(super.append(p.toString(), null));
                hash.put(idList, idRecord);
            }
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeRS();
        }


    }

    private void openRS() {
        try {
            rs = RecordStore.openRecordStore("Users", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    private void closeRS() {
        try {
            rs.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }  
}
Это было полезно?

Решение

Record ID's are unique identifiers that are added along with the inserted data by the RMS system. Record ID is 1 for the first record and gets incremented by one on every addition of a new record. If any record is deleted from the Record Store the Record ID's will not be reset in sequential order.This deleted record and its ID is lost forever. We cannot reuse the deleted record ID. That is, if we have three rows in a table with the IDs 1, 2, and 3, deleting ID 2 will remove this identifier permanently from the record store.And records with ID's 1,3 will remain in recordstore.If we were to add another row to this table, it would have an identifier of 4.So when you delete record with ID 1,first non deleted record'ID will be 2.
I usually open recordstore,read it's data and write them in a vector then delete,insert, ... records in vector(instead of recordstore) and finally write remained records to(even a new) recordstore.
References:
http://www.ibm.com/developerworks/library/j-j2me3/
http://www.j2mesalsa.com/elearning/rms.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top