2014-12-12 1 views
0

저는 독립 연구를위한 간단한 노트 작성 앱을 만들고 있습니다. 문제는 앱이 종료 될 때마다 생성 된 메모가 삭제되고 앱이 완전히 재설정된다는 것입니다. 몇 가지 튜토리얼 튜토리얼과 saveoninstance 메소드를 읽었지 만, 구현하려는 여러 가지 방법이 무엇이든 관계없이 알아낼 수는 없습니다.Android Studio에서 내부적으로 수집하는 데이터를 어떻게 저장합니까?

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); 

    } 

이것은 수집 된 문자열을 저장하지 않고 코드입니다. 누군가가이 데이터를 저장하는 방법 중 하나를 구현하는 방법을 알아낼 수 있도록 도와 주시겠습니까? 그래서 앱이 삭제 된 후에 검색 할 수 있습니까? 에,

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(); 

은 물론 당신이 당신의 응용 프로그램을 열 때 파일에 읽기해야 할 것 :

+1

당신이 개발자 페이지 [저장 옵션]을 참조 수 (http://developer.android.com/guide/topics /data/data-storage.html). –

+0

메모를 텍스트 파일이나 데이터베이스 항목으로 저장해야합니다. –

답변

0

하나의 옵션은 내부 저장 장치에 파일로 문자열을 작성하는 것입니다 이전에 저장된 메모를 설정하려면 :

  1. openFileInput()을 호출하고 읽을 파일의 이름을 전달하십시오. 이것은 FileInputStream을 반환합니다.
  2. read()로 파일의 바이트를 읽습니다.
  3. 그런 다음 close()를 사용하여 스트림을 닫습니다.
당신은 문자열로 FileInputStream의 변환에 도움이 정보를 찾을 수 있습니다

: How to convert FileInputStream into string in java?

관련 문제