【问题标题】:How do I save the data I'm collecting internally in Android Studio?如何保存在 Android Studio 内部收集的数据?
【发布时间】:2015-12-17 02:08:34
【问题描述】:

我正在为我的独立学习创建一个简单的笔记应用程序。问题是任何时候退出应用程序,创建的笔记都会被删除,应用程序会完全重置。我已经阅读了一些关于偏好和 saveoninstance 方法的教程,但是无论我尝试使用多少不同的方式来实现它们,我似乎都无法弄清楚。

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

    }

这是没有保存收集到的字符串的代码。有人可以帮我弄清楚如何实现其中一种方法来保存此数据,以便在应用程序被销毁后可以检索它吗?

【问题讨论】:

  • 您可以查阅开发者页面以获取storage options
  • 您应该将笔记保存为文本文件或数据库条目。

标签: java android save


【解决方案1】:

一种选择是将字符串作为文件写入内部存储:

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. 调用 openFileInput() 并将要读取的文件的名称传递给它。这将返回一个 FileInputStream。
  2. 使用 read() 从文件中读取字节。
  3. 然后使用 close() 关闭流。

您可能会发现此信息有助于将 fileInputStream 转换为字符串: How to convert FileInputStream into string in java?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多