AaronRogers3
AaronRogers3

Reputation: 21

Reading Data from Internal Storage

I am working with Java in Android Studio to create a mobile app for the business i am working on placement for. I have ran into a problem when trying to store data using Internal memory.

Basically, once the user clicks on a "Start button" i am hoping to saves the users selections behind the scenes to a file and then on the next screen retrieve the stored selections.

Here is the code i have for writing the data to the file (which seems to be working):

            int spinnerSelection = myspinner.getSelectedItemPosition();
            int spinnerSelection2 = myspinner2.getSelectedItemPosition();
            String q = quantity.getText().toString();
            String d = duration.getText().toString();
            String formattedtime = fmt.print(dateTime);
            TextView endtime = findViewById(endtimetextView);


            String file_name = "Records.txt";
            File file = null;
            FileOutputStream fileOutputStream = null;
            try {
                file = getFilesDir();
                fileOutputStream = openFileOutput(file_name, MODE_PRIVATE);

                fileOutputStream.write(spinnerSelection);
                fileOutputStream.write(spinnerSelection2);
                fileOutputStream.write(q.getBytes());
                fileOutputStream.write(d.getBytes());
                fileOutputStream.write(endtimecalc.getBytes());
                fileOutputStream.write(formattedtime.getBytes());

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            Toast.makeText(screen2.this, "SUCESSFULLY SAVED TO " + file, Toast.LENGTH_SHORT).show();

        }
    });

Please let me know if i need to change any of this code (in particular im not to sure if this is the correct way to save a spinner value which has been selected).

Now, for reading the data into the next screen, i have just been following some different youtube tutorials and none of them have worked out.

Here is some code ive tried:

TextView DisplaySaved;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen4);

    DisplaySaved = (TextView) findViewById(R.id.textView2);
}


public void read (View view) {

    try {
        FileInputStream fileInputStream = openFileInput("Record.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer buffer = new StringBuffer();
        String lines;

        while ((lines = bufferedReader.readLine()) !=null) {
            buffer.append(lines+"\n");
        }

        DisplaySaved.setText(buffer.toString());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Also in addition, would anyone know how to create a new record each time instead of it actually overwriting the same file each time? its just i need to store each time a record is created

Please if anyone could help me out to correct this it would be great.

Thanks!

Upvotes: 0

Views: 607

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80914

you need to use intent to pass data through activities example this:

 Intent intent = new Intent(v.getContext(), SignoutActivity.class);
 intent.putExtra("EXTRA_SESSION_ID", sessionId); //putExtra method used to 
send data between activities
 startActivity(intent); //starts activity

Extra_SESSION_ID is a key that you declare and sessionId is the value that you want to pass.

Then in the second activity use this:

String s = getIntent().getStringExtra("EXTRA_SESSION_ID");

use getIntent() to get the intent and getStringExtra(..) method to obtain the value in the second activity.

Upvotes: 1

alexay68
alexay68

Reputation: 172

I don't know if you absolutely need to use files to store this informations, but there are many others way to pass information to a new Activity with Android Studio.

You should check the Intent class, you can send a Bundle with your data inside !

Upvotes: 1

Related Questions