0

I am making an app where I have my school balance set and can press dedicated buttons to take away certain amounts depending on what I purchase. I am having trouble with the balance. I have a way of updating it to what I want, however after I terminate the application I want it to stay the same in a TextView. I so far was able to make it save it to a file, or at least I hope I was, with the help of a video.

How could I on opening the app read the text file and set the TextView equal to what is in the file?

submitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String submitAmount = amountEnter.getText().toString();
            balance.setText(submitAmount);

            String myBalance = balance.getText().toString();

            try {
                FileOutputStream fou = openFileOutput("balance.txt", 
MODE_WORLD_READABLE);
                OutputStreamWriter osw = new OutputStreamWriter(fou);
                try {

                    osw.write(myBalance);
                    osw.flush();
                    osw.close();

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

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

        }
    });
Alex Wiz
  • 3
  • 4
  • 1
    You could use `SharedPreference` to store your data. Here's the official android documentation: [SharedPreference](https://developer.android.com/training/basics/data-storage/shared-preferences.html) – Pulak Jun 12 '17 at 17:53
  • where is your file located? SD Card or internal memory? – Onur A. Jun 12 '17 at 18:03
  • Saving data on a file could be dangerous because the file can be moved or deleted (or worse). Consider using SharedPreferences – Thecave3 Jun 12 '17 at 19:40

2 Answers2

1

Seeing your code, i assume that you know how to read/write a file in android. If not, then see it from here . https://stackoverflow.com/questions/12421814/how-can-i-read-a-text-file-in-android/ You can try the following code. THe readfile method is from the upper link. You just have to read the particular line/string from the file at onCreate method of the activity. Get the reference of your desired TextView and then set the text to TextView like below. I hope it helps you

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
        String texts = readfile();
        TextView textView = findViewById(R.id.text_view);
        textView.setText(text);


}

private String readfile() {
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard, "file.txt");
    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
        br.close();
    } catch (IOException e) {
        //You'll need to add proper error handling here
    }

    return text.toString();
}
Md. Sulayman
  • 781
  • 8
  • 30
1

You can save it in SharedPrefs change it to

submitBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String submitAmount = amountEnter.getText().toString();
        balance.setText(submitAmount);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).
edit().putString("balance", submitAmount).commit();

    }
});

and you can get it by

String balance=PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.getString("balance","nothing_there"); 
Adithya
  • 1,688
  • 1
  • 10
  • 18