views:

47

answers:

1

Hello, 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.

Thanks

+1  A: 

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.

Yoni Samlan
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?
John
Sure, though possibly more work than just splitting a String because you'd also have to iterate back through all your sharedPreferences for deserialization.
Yoni Samlan
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.
John