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?

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

How does this apply to saving app data, e.g. SharedPreferences?
putStringSet(String key, Set<String> values) Set a set of String values in the preferences editor, to be written back once commit() is called. SharedPreferences.Editor
1

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

2 Comments

How does this apply to saving app data, e.g. SharedPreferences?
Thanks. I do have an ArrayList<String[]> though :-P is there an elegant way of storing that as an String-Array?
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.

3 Comments

wow, so I just have to call super.onSaveInstanceState and super.onRestoreInstanceState. And everything will be saved and loaded automatically???
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.
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?
0

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

After serialization, store with:

putByteArray("key", serializedData);

Comments

0

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

4 Comments

but this is only for when i want to transfer an object something to another activity, right? it doesnt save anything, does it?
Sorry, I misinterpreted your question. Parcelable is useful for passing data across activities or process.
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 stackoverflow.com/questions/134492/…) 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)
what does 'really large' mean? its an arraylist that has around 20-100 String[]-entries. which only have small strings in them (short sentences)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.