RMS からのレコードを表示する際に問題が発生しました。j2me 用の電子ウォレット アプリケーションを作成しています。これが私のソース コードです。recordwith#id が追加されたとのことですが、ユーザーからの収入額の取得と表示に失敗しました。これは私のRMSのコードです
//variable for record store
RecordStore ew_rs;
public static int addCount = 0;
public static int editCount = 0;
public static int deleteCount = 0;
public addIncome(){
this("tmpfile");
}
public addIncome(String filename) {
super();
try{
ew_rs = RecordStore.openRecordStore(filename, true);
ew_rs.addRecordListener(this);
} catch (RecordStoreException e){
e.printStackTrace();
}
}
public void closeRMS(){
try {
if (ew_rs.getNumRecords() > 0) {
ew_rs.closeRecordStore();
} else {
ew_rs.closeRecordStore();
RecordStore.deleteRecordStore(ew_rs.getName());
}
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
//variable declaration for income adding
int ew_incomeID;
float ew_incomeAmount=0;
public addIncome(int incomeID, float incomeAmount){
this.ew_incomeID = incomeID;
this.ew_incomeAmount = incomeAmount;
}
public int getIncID(){
return ew_incomeID;
}
public void setIncID(int incomeID){
this.ew_incomeID = incomeID;
}
public float getIncAmt(){
return ew_incomeAmount;
}
public void setIncAmt(float incomeAmt){
this.ew_incomeAmount = incomeAmt;
}
public void recordAdded(RecordStore recordStore, int recordId) {
addCount++;
try {
System.out.println("Record with ID# " + recordId +
" added in Record Store " + recordStore.getName());
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void recordChanged(RecordStore recordStore, int recordId) {
editCount++;
try {
System.out.println("Record with ID# " + recordId +
" changed in Record Store " + recordStore.getName());
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void recordDeleted(RecordStore recordStore, int recordId) {
deleteCount++;
try {
System.out.println("Record with ID# " + recordId +
" deleted in Record Store " + recordStore.getName());
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//add income
public void addIncomeFunction(int id , float IncAmt){
id = getIncID();
IncAmt = getIncAmt();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
int convertAmt = Float.floatToIntBits(IncAmt);
try {
out.writeUTF(String.valueOf(id));
out.writeInt(convertAmt);
}catch (IOException e) {
e.printStackTrace();
}
byte[] b = baos.toByteArray();
try {
ew_rs.addRecord(b, 0, b.length);
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
baos.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//retrieve income
public byte[] getIncome(){
byte[] read = null;
try{
read = new byte[ew_rs.getSize()];
RecordEnumeration re = ew_rs.enumerateRecords(null, null, false);
ByteArrayInputStream bais = new ByteArrayInputStream(re.nextRecord());
DataInputStream in = new DataInputStream(bais);
for (int i = 0; i < re.numRecords() && re.hasNextElement(); i++) {
try {
in.readInt();
Float.intBitsToFloat(in.readInt());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return read;
}
public RecordEnumeration enumerate(){
try {
return ew_rs.enumerateRecords(null, null, false);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
そして私のディスプレイのコード
MainMenu m;
addIncome income;
public Display d;
private static final Command cmdOk = new Command("Ok", Command.OK, 1);
private static final Command cmdBack = new Command("Back", Command.OK, 1);
public BalanceForm(MainMenu m){
this("Balance", m);
this.addCommand(cmdOk);
this.addCommand(cmdBack);
this.setCommandListener(this);
}
public BalanceForm(String arg0, MainMenu m){
super(arg0);
this.m = m;
}
public void prepareView(Display display){
this.setCommandListener(this);
this.d = display;
}
public void showView(){
d.setCurrent(this);
}
public void commandAction(Command c, Displayable d) {
if (c == cmdBack)
{
m.showMenu();
}
else{
byte[] b = income.getIncome();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
DataInputStream in = new DataInputStream(bais);
try {
System.out.println(in.readInt());
System.out.println(in.readUTF());
//this.append(in.readUTF());
//this.append(String.valueOf(in.readInt()));
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}