简体   繁体   中英

How should i save my app data in my android app?

I am making my first android app. I want to save an array of strings in one activity and then in second activity i want to show those strings as a list. I am using android platform 2.2 with API 8 So i can not use putStringSet(). Is there any way to save my data as a text file in my app? Then may be i can just add a new line in that file whenever user adds a new string. And while making list view i can parse it on basis of new line and make string array. Thanks for your help.

Use File Handling. Easy to use for beginners and efficient for your purpose.

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

There is a method putStringArrayListExtra("id", ArrayList list) in Intent to use before starting it. Then in the launched Activity from the Intent, use getStringArrayList("id") .

For example:

Intent intent = new Intent(MainActivity.this.getApplicationContext(), NewActivity.class);
intent.putStringArrayListExtra("id", yourArrayList)
MainActivity.this.startActivity(intent);

Then on NewActivity onCreate() method

ArrayList<String> list = getIntent().getExtras().getStringArrayList("id");

But you can use putStringArray, or putStringArrayList wrapped into a Bundle.

Bundle are passed via Intents.

-> http://developer.android.com/reference/android/os/Bundle.html#putStringArray(java.lang.String , java.lang.String[])

Then, if you want to save it as a file, then you have to use this method : http://www.java-forums.org/advanced-java/13852-saving-arraylist-file.html

But i think you prefer pass the String list directly.

Why are you using files for that? Just check sharedPreferences and sqliteDb It may work for you..shared preferences are easy to handle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM