0

I have a problem with my code, I need to read EditText from a user and than put that text into another layout and do some calculations. My problem is that I don't understand how to read/write data in Android. Below is a snippet of the read/write code.

Thanks for the help!

EDIT: Should be noted that all of this is inside of my MainActivity class

public EditText editName;
public EditText editAMT;
public Button save;

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

    editName = (EditText) findViewById(R.id.editName);
    editAMT = (EditText) findViewById(R.id.editAMT);
    save = (Button) findViewById(R.id.save);


    File fName = new File(name);
    File fAMT = new File(AMT);

    //WRITING TO THE FILE
    try{
        FileOutputStream test = openFileOutput(name, Context.MODE_PRIVATE);
        FileOutputStream test2 = openFileOutput(AMT, Context.MODE_PRIVATE);

        test.write(editName.getText().toString().getBytes());
        test2.write(editAMT.getText().toString().getBytes());

        test.close();
        test2.close();

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

    //READING FROM THE FILE

    try{
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput(name)));
        String inputString;
        StringBuffer stringBuffer = new StringBuffer();

        while ((inputString = inputReader.readLine()) != null){
            stringBuffer.append(inputString + "\n");
        }

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

}

EDIT: Here is my action plan.

1) Get information from EditText called (eAMT). 2) Get information from EditText called (eName). 3) Put the information of eAMT into a file called eName.txt 4) In another activity, search for the file called eName. 5) Once search is completed, pull the contents of that file and display onto another activity (Main activity).

  • 1
    Your question is **not** Android Studio related. Tag removed. Do not spam with unrelated tags! – Marcin Orlowski Apr 26 '16 at 20:19
  • 1
    Retrieving text from an EditText, setting text in other layouts, and saving data to disk are all separate operations. Please clarify what it is you are actually trying to accomplish and state specifically (including any related error outputs) how that is not happening for you. – NoChinDeluxe Apr 26 '16 at 20:20
  • You can find a similar answer for your question in this thread on SO http://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android – e2a Apr 26 '16 at 20:20
  • You don't need to persist the values in a file. `SharedPreferences` might also be worth a look. – user3105453 Apr 26 '16 at 20:22
  • @NoChinDeluxe I have completed the retrieving text from EditText. I have that and stored away in a String. Now I am working on pushing that string into a file. So far, I do not understand on how to pass a file to another activity. I understand how to pass strings and other variables, but not files. – Evan Tempel Apr 27 '16 at 20:57

1 Answers1

1

Instead of writing to a file, use Android's SharedPreferences:

SharedPreferences preferences = getSharedPreferences("com.example.myapp", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("UniqueEditNameKey", editName.getText().toString);

From another activity, you can do the following to retrieve the value:

SharedPreferences preferences = getSharedPreferences("com.example.myapp", MODE_PRIVATE);
String name = preferences.getString("UniqueEditNameKey", ""); // second argument is default value if key does not exist
AgileNinja
  • 914
  • 1
  • 7
  • 20
  • I would preferable would not want to do SharedPreferences. I need it to be a file. I need to be able to select a file and get the information from it. I have made progress since this post. – Evan Tempel Apr 27 '16 at 20:54