• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Save calculator result problem

bgoody

Well-Known Member
Aug 18, 2009
127
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]
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones