package com.twolf.stressball11; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; /**************************************************************************** * In this class you will be able to load previously saved journal entries and display them to * the screen in a scrollable textbox. The spinner(drop down box) is loaded from a file that * keeps just the date time stamps of the files to load on click. I used everything I gathered from * all of the sources and came up with this method of reading from a file with what I knew at this * point from creating all other activities. * * -Thomas Wolf */ public class Journal extends AppCompatActivity { TextView textView; Spinner spin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_journal); textView=(TextView) findViewById(R.id.savedData); spin=(Spinner)findViewById(R.id.spinner); SharedPreferences sharedPref= getSharedPreferences("numberSaves", Context.MODE_PRIVATE); int spinnerData= sharedPref.getInt("saves",1);//if file was not found will return /* spinnerData will=1 by default so if no file has been saved yet the spinnerData will be equal to 1. When the save button is clicked in the Log activity this will be incremented https://www.youtube.com/watch?v=HR5eb_oWIEc ***filling the spinner ^^ extremely helpful in the filling of the spinner */ if(spinnerData>=2) { String[] yolo =new String[spinnerData-1];// set array size account for the default of 1 fillSpinner(yolo); ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,yolo); spin.setAdapter(myAdapter); } } /*********************************************************************************** * Function Name: onResume() * Purpose: When you return to this activity from the log page the spinner needs to be filled * so you dont have to exit the activity and reload it the onResume method will check to see * if something was saved and then load it into the spinner * Usage: called whenever you return to this activity * * helpfull: http://stackoverflow.com/questions/5545217/back-button-and-refreshing-previous-activity */ @Override public void onResume() { // After a pause OR at startup super.onResume(); SharedPreferences sharedPref= getSharedPreferences("numberSaves", Context.MODE_PRIVATE); int spinnerData= sharedPref.getInt("saves",1);//if file was not found will return if(spinnerData>=2) { String[] yolo =new String[spinnerData-1]; fillSpinner(yolo); ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,yolo); spin.setAdapter(myAdapter); } } //starts Log activity public void newEntry(View v) { Intent i=new Intent(Journal.this, Log.class); startActivity(i); } /***************************************************************************************** * Function Name: writeData(View v) * Purpose when the load btton is clicked this function will first check to make sure there * is something saved in the spinnerdata. If nothing is saved then the load button will do * nothing and not error out. When a file is saved and loaded into the spinner it will * automatically be selected. * Usage: called when the load button is clicked */ public void writeData (View v) { SharedPreferences sharedPref= getSharedPreferences("numberSaves", Context.MODE_PRIVATE); int spinnerData= sharedPref.getInt("saves",1);//if file was not found will return 1 try { if(spinnerData>=2) { //Make sure there is something to load String message; String slected = spin.getSelectedItem().toString();//get the file name from spinner FileInputStream fileInputStream = openFileInput(slected); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuffer stringBuffer = new StringBuffer(); while ((message = bufferedReader.readLine()) != null) { stringBuffer.append(message + '\n'); } textView.setText(stringBuffer.toString()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /************************************************************************************] * Function Name: fillSPinner(String[] ) * Purpose: This function will go into the file that has all the time stamps saved to it * and load them into the array. When it returns the array will be used to load the spinner * Usage: called when returning to the Journal activity and when the Journal Activity loads */ public void fillSpinner(String[] yolo) { try { String data; int i=0; FileInputStream fis = openFileInput("savedDates"); InputStreamReader inputStreamReader =new InputStreamReader(fis); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuffer stringBuffer = new StringBuffer(); while((data=bufferedReader.readLine())!=null) { yolo[i]=data; //stringBuffer = new StringBuffer(); i++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }