j2me delete record from recordstore
Problem:
unable to use deleteRecord()
properly
Background: I have a simple j2me application where I add various strings to the record store and try to manipulate the contents of the record store. I add records on o开发者_JS百科ne screen and then read them on another. I can move back and forth with these screens
Problem description in detail:
For example I add "abc" (recordID is 1) , "def" ( id is 2) and "ghi" ( id is 3).
When I delete these records in the order rs.deleteRecord(3),rs.deleteRecord(2),rs.deleteRecord(1)
everything works like it is supposed to.
When I try any other order I get "Msg: javax.microedition.rms.InvalidRecordIDException" Also when I try to read the other records after this deletion there is no output.
I want to be able remove the records in any order.
Thanks in advance
Open RecordStore
that name is rs...
And then...
RecordEnumeration re=null;
try {
re = rs.enumerateRecords(null, null, true);
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
int rid=0;
try {
while (re.hasNextElement()) {
rid=re.nextRecordId();
try {
rs.deleteRecord(rid);
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
} catch (InvalidRecordIDException ex) {
ex.printStackTrace();
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
} catch (InvalidRecordIDException ex) {
ex.printStackTrace();
}
I faced the same problem. But after carefully reading the api class and this method I found a line written in the explanation that "The record is deleted from the record store. The recordId for this record is NOT reused." Hence to update and add a specific data at the same record number is not possible in recordstore! Therefore you either need to use Record enumeration technique or use a class with the required fields you want to store , convert it into ByteArray and then store the ByteArray in a record store. Hope this would help you and others.
精彩评论