1

我在显示来自 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();
        }
    }
}

}

4

0 回答 0