2

I created a weather widget. I store its configuration in SharedPreferences. The widget is updated by a service. I keep the weather information together with forecasts in an array. After the phone is off for the night i find that the array values are gone, maybe the system suspended? the service. Is there a way to store the array in SharePreferences so it is more sticky.

1

1 Answer 1

4

Sure - either come up with a way to serialize/deserialize your object from a String that you can store in a SharedPreference (if it's a simple array of numbers, for instance, just making a quick split/join on commas might do it; basic JSON serialization/deserialization might work for slightly more complex data), or use standard Java serialization to turn your object into a bytestream and save the data to a File that you can read when needed. If you're really working with a lot of structured data, though, seriously consider using Android's excellent built-in support for SQLite, although that's a little more work.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for quick response. I don't know the serialization, but was wondering, because is an array of strings, to iterate through the array and put each element as a pair in the SharedPreferences. So I could use for(i=0;i<wf.length;i++) {edit.putString("wf"+i,1);} Is that acceptable?
Sure, though possibly more work than just splitting a String because you'd also have to iterate back through all your sharedPreferences for deserialization.
Yes, it would be more work, any example on the web to serialize an array of strings and then back again? Thanks for all the info.
Why do you think "structured data" should be stroed in sqlite? Sqlite (and relational db iin general) are meant to keep relational data - not structured data. I'd say that structured data are exactly opposite of relational data.

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.