I couldn't get that one to work so I'm trying something else.

Here's where the button is pressed:

btnMP.setOnClickListener(new Button.OnClickListener() {
                        public void onClick(View v) {
                        memNum = memNum + 
Double.parseDouble(txtCalc.getText().toString());
                        operator = 0;   }
                });

Here's a thingie I hammered together from parts:

// 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;

              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();
                            }
                  }
             }

This is called by inserting WriteMemory(this,memNum); in the button
routine thusly:

btnMP.setOnClickListener(new Button.OnClickListener() {
                        public void onClick(View v) {
                        memNum = memNum + 
Double.parseDouble(txtCalc.getText().toString());
                        operator = 0;   }
                        WriteMemory(this,memNum);
                });

The file is created but nothing is written in it. What's missing?
Brian

On Sep 28, 3:38 pm, "Yusuf Saib (T-Mobile USA)" <yusuf.s...@t-
Mobile.com> wrote:
> txt is declared inside your "if (hasChanged) {" scope. Then you use it
> after the corresponding "}".
>
> Yusuf Saib
> Android
> ·T· · ·Mobile· stick together
> The views, opinions and statements in this email are those of the
> author solely in their individual capacity, and do not necessarily
> represent those of T-Mobile USA, Inc.
>
> On Sep 27, 10:02 am, bgoody <bgoody...@gmail.com> wrote:
>
> > Hi. I am trying to hack this bit of code tosavethe results of a
> > calculation to disk but it says that the variable (txt) cannot be
> > resolved.
> > Any ideas!
>
> > private void handleEquals(int newOperator) {
> > if (hasChanged) {
> > switch (operator) {
> > case 1:
> > num = num +Double.parseDouble(txtCalc.getText().toString());
> > break;
> > case 2:
> > num = num -Double.parseDouble(txtCalc.getText().toString());
> > break;
> > case 3:
> > num = num *Double.parseDouble(txtCalc.getText().toString());
> > break;
> > case 4:
> > num = num /Double.parseDouble(txtCalc.getText().toString());
> > break;
>
> > }
>
> > String txt =Double.toString(num);
> > txtCalc.setText(txt);
> > txtCalc.setSelection(txt.length());
>
> > readyToClear = true;
> > hasChanged = false;
>
> > }
>
> > FileOutputStream fOut = openFileOutput
> > ("samplefile.txt",MODE_WORLD_READABL E);
> > OutputStreamWriter osw = new OutputStreamWriter(fOut);
> > osw.write(txt);
> > osw.flush();
> > fOut.close();
> > osw.close();
> > operator = newOperator;
>
> > }
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-beginners@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to