开发者

Android Sharedpreferences and array

I created a weather widget. I store its configur开发者_JAVA技巧ation 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜