Android Sharedpreferences和数组 [英] Android Sharedpreferences and array

查看:141
本文介绍了Android Sharedpreferences和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个天气小部件.我将其配置存储在SharedPreferences中.小部件由服务更新.我将天气信息与天气预报一起保存在一个数组中.电话通宵关机后,我发现数组值不见了,也许系统已挂起?服务.有没有一种方法可以将数组存储在SharePreferences中,从而使其更粘.

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.

推荐答案

可以-要么想出一种方法来对对象进行序列化/反序列化,然后将其存储在SharedPreference中即可(如果是简单的数字数组,例如,只需对逗号进行快速拆分/合并即可;基本 JSON序列化/反序列化可能适用于稍微复杂一些的数据),或使用标准的 Java序列化以将您的对象转换为字节流并保存数据保存到文件中,您可以在需要时读取该文件.但是,如果您确实要处理大量结构化数据,则请认真考虑使用Android对SQLite的出色内置支持,尽管这需要做更多工作.

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.

这篇关于Android Sharedpreferences和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆