安卓共享偏好和阵列[英] Android Sharepreferences and array

本文是小编为大家收集整理的关于安卓共享偏好和阵列的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我创建了一个天气小部件.我将其配置存储在SharedPreferences中.窗口小部件由服务更新.我将天气信息与阵列中的预测一起保留在一起.在手机休息后,我发现阵列值消失,也许系统暂停?服务.有没有办法将数组存储在SharePreferences中,所以它更加粘.

推荐答案

旦上来,可以从一个可以存储在SharedPreference中的字符串中序列化/反序列化对象的方法(如果它是一个简单的数字数组,例如,只需快速分割/加入逗号可能会这样做;基本 json serialization /deserialization可能适用于稍微复杂的数据),或使用标准 java serialization 将您的对象变成一个bytestream和将数据保存到文件那个你可以在需要时阅读.但是,如果您真的使用了很多结构化数据,请认真考虑使用Android对SQLite的卓越内置支持,虽然这是一点工作.

本文地址:https://www.itbaoku.cn/post/102479.html

问题描述

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.

推荐答案

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.