0

I am creating a simple note-taking app for my independent study. The problem is that any time the app is exited, the notes created are deleted and the app is completely reset. I have read several tutorials of preferences and the saveoninstance methods, but no matter how many different ways I try to implement them, I can't seem to figure it out.

public class Home extends Activity {

//Declaration of variables
private Button mNoteButton;
private String mText;
private ListView myList;
private int index;
public static final String TAG = Note.class.getSimpleName();
ArrayList<String> myArrayList= new ArrayList<String>();
ArrayAdapter<String> myAdapter;

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



    //Setting the list item to blank
    if (mText == null) {
        mText = "";
    }
    Log.d(TAG, mText);



    //creating adapter to insert myListArray into the ListView
    myAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myArrayList);

    //Setting the adapter to myArrayList
    myList= (ListView) findViewById(R.id.noteList);
    myList.setAdapter(myAdapter);

    //Creating and setting the new note button
    mNoteButton = (Button)findViewById(R.id.newNoteButton);
    //When button is clicked
    mNoteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            newNote();
        }
    });

    //When an item in the list is clicked
    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Taking the data in the item selected and sending it to the EditNote.java
            index = position;
            //Selecting item data

            String old = myArrayList.get(index);

            Intent intent = new Intent(Home.this, EditNote.class);
            intent.putExtra("note_text", old);
            startActivityForResult(intent, 1);

        }
    });
}


private void newNote()
{
    //Starts and sends data to Note.java and creates a new note
    Intent intent = new Intent(this, Note.class);
    startActivityForResult(intent, 0);

}

protected void onActivityResult(int requestCode, int resultCode, Intent DATA) {
    super.onActivityResult(requestCode, resultCode, DATA);

    //Data from the Note activity is received
    if (requestCode == 1 && resultCode == RESULT_OK)
    {
        //Gets data and saves it to myListArray as new edit
        Bundle save = DATA.getExtras();
        String extra = save.getString("note");
        myArrayList.set(index, extra);
        myList.setAdapter(myAdapter);

    }
    if (requestCode == 0 && resultCode == RESULT_OK) {
        //Gets data and saves it to myListArray as new note
        Bundle pack = DATA.getExtras();
        String pageText = pack.getString("note");
        myArrayList.add(pageText);
        myList.setAdapter(myAdapter);

    }

This is the code without saving the collected strings. Can someone please help me figure out how to implement one of the methods to save this data so it can be retrieved after the app is destroyed?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

One option is to write the string as a file to internal storage:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

String FILENAME = "yourFileName";
String extra = save.getString("note");
FileOutputStream fileOutputStream = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fileOutputStream.write(extra.getBytes());
fileOutputStream.close();

Also of course you are going to have to read in the file when you open your app, in order to set any notes that were previously saved:

  1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
  2. Read bytes from the file with read().
  3. Then close the stream with close().

You might find this info helpful on converting a fileInputStream to a string: How to convert FileInputStream into string in java?

Community
  • 1
  • 1
joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30