4

Is it possible to save an entire array (or even ArrayList) to the android app data?

As far as I know you can only do stuff like putInt, putBoolean or putString.... But what about more complex data-types? Is there a way to do that?

Or do I have to convert the whole array to a String first, and then parse it to an array again on load?

Roman Melnyk
  • 1,097
  • 1
  • 8
  • 21
RazorHail
  • 431
  • 1
  • 5
  • 17

5 Answers5

3

for complex data-types use

            Bundle bundle = new Bundle();
            bundle.putSerializable("key", object);
            intent.putExtra("bundle", bundle);

EDit: or for Arrays use

   putStringArray(key,value);
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 2
    How does this apply to saving app data, e.g. SharedPreferences? – Peter Knego Aug 29 '11 at 14:23
  • putStringSet(String key, Set values) Set a set of String values in the preferences editor, to be written back once commit() is called. [SharedPreferences.Editor](http://developer.android.com/reference/android/content/SharedPreferences.Editor.html) – user370305 Aug 29 '11 at 15:57
1

I think yuou dont know there is also functions like putFloatArray(key,Value),putStringArray(key,value).You can use those.

Android Killer
  • 18,174
  • 13
  • 67
  • 90
1

If by "app data" you mean SharedPreferences, then no, you can only save simple types.

However you can save application state via Activities' onSaveInstanceState / onRestoreInstanceState as described here: Saving Android Activity state using Save Instance State

Note that this saves state per Activity, so it's best to have a main activity where you save app state.

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • wow, so I just have to call super.onSaveInstanceState and super.onRestoreInstanceState. And everything will be saved and loaded automatically??? – RazorHail Aug 29 '11 at 14:27
  • No, you just have to implement those methods in your Activity and they will be called automatically. No need to call then manually. See the link. – Peter Knego Aug 29 '11 at 14:31
  • oh well....then that doesnt really help me, does it? :) I wanted to be able to save more complex-data-types. those methods are just the ones that are called for saving and loading. but how (and where) do I actually save an Object? – RazorHail Aug 29 '11 at 15:28
0

You can serialize almost any form of data and save it as a byte stream byte[]

After serialization, store with:

putByteArray("key", serializedData);
Bosah Chude
  • 3,670
  • 2
  • 22
  • 22
0

You need to implement Parcelable. You should have a look at this tutorial Passing ArrayList across activities

hleroy
  • 463
  • 4
  • 10
  • but this is only for when i want to transfer an object something to another activity, right? it doesnt save anything, does it? – RazorHail Aug 29 '11 at 15:25
  • Sorry, I misinterpreted your question. Parcelable is useful for passing data across activities or process. – hleroy Aug 29 '11 at 16:36
  • If your array contains primitive type, iterate over it and save it in SharedPreferences. If your array or ArrayList is more complex, serialize it to a base64 string (see http://stackoverflow.com/questions/134492/how-to-serialize-an-object-into-a-string) and save it as a string in SharedPreference. If your ArrayList is really large, consider using other form of persistent storage (writing to a local file or using SQLite) – hleroy Aug 29 '11 at 16:42
  • what does 'really large' mean? its an arraylist that has around 20-100 String[]-entries. which only have small strings in them (short sentences) – RazorHail Aug 30 '11 at 12:00