Save calculator result problem


Last Updated:

  1. bgoody

    bgoody Well-Known Member This Topic's Starter

    Joined:
    Aug 18, 2009
    Messages:
    127
    Likes Received:
    6
    [FONT=Courier, Monospaced]Here's where the button is pressed:
    [/FONT]
    [FONT=Courier, Monospaced]btnMP.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
    memNum = memNum + Double.parseDouble(txtCalc.getText().toString());
    operator = 0; }
    });
    [/FONT]
    [FONT=Courier, Monospaced]Here's a thingie I hammered together from parts:
    [/FONT]
    [FONT=Courier, Monospaced]// Save MEMORY
    public void WriteMemory(OnClickListener onClickListener, double
    memNum){
    File sdcard = Environment.getExternalStorageDirectory();
    File destinationdir = new File(sdcard,"/download/calculator");
    destinationdir.mkdir();
    File destinationfile = new File(destinationdir,"memory.dat");
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;
    [/FONT]
    [FONT=Courier, Monospaced] try{
    destinationfile.createNewFile();
    fOut = new FileOutputStream(destinationfile);
    osw = new OutputStreamWriter(fOut);
    osw.write((int) memNum);
    osw.flush();
    Toast.makeText((Context) onClickListener, "Memory
    saved",Toast.LENGTH_SHORT).show();
    }
    catch (Exception e) {
    e.printStackTrace();
    // Toast.makeText((Context) onClickListener, "Memory not
    saved",Toast.LENGTH_SHORT).show();
    }
    finally {
    try {
    osw.close();
    fOut.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    [/FONT]
    [FONT=Courier, Monospaced]This is called by inserting WriteMemory(this,memNum); in the button
    routine thusly:
    [/FONT]
    [FONT=Courier, Monospaced]btnMP.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
    memNum = memNum + Double.parseDouble(txtCalc.getText().toString());
    operator = 0; }
    WriteMemory(this,memNum);
    });
    [/FONT]
    [FONT=Courier, Monospaced]The file is created but nothing is written in it. What's missing?
    Brian [/FONT]
     

    Advertisement

Share This Page

Loading...