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