将SparseBooleanArray保存到SharedPreferences(Save SparseBooleanArray to SharedPreferences)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
将SparseBooleanArray保存到SharedPreferences(Save SparseBooleanArray to SharedPreferences)

对于我的应用程序,我需要将一个简单的SparseBooleanArray保存到内存中,然后再读取它。 有什么办法使用SharedPreferences保存它?

我考虑过使用SQLite数据库,但看起来像这样简单。 我在StackOverflow上找到的一些其他答案建议使用GSON将其保存为字符串,但我需要保持此应用程序非常轻和快速的文件大小。 有没有任何方法可以实现这一点,而不依赖于第三方库,同时保持良好的性能?

For my app, I need to save a simple SparseBooleanArray to memory and read it later. Is there any way to save it using SharedPreferences?

I considered using an SQLite database but it seemed overkill for something as simple as this. Some other answers I found on StackOverflow suggested using GSON for saving it as a String but I need to keep this app very light and fast in file size. Is there any way of achieving this without relying on a third party library and while maintaining good performance?

最满意答案

您可以使用JSON的强大功能来保存任何类型对象的共享首选项

例如SparseIntArray保存项目,如Json字符串

public static void saveArrayPref(Context context, String prefKey, SparseIntArray intDict) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = prefs.edit(); JSONArray json = new JSONArray(); StringBuffer data = new StringBuffer().append("["); for(int i = 0; i < intDict.size(); i++) { data.append("{") .append("\"key\": ") .append(intDict.keyAt(i)).append(",") .append("\"order\": ") .append(intDict.valueAt(i)) .append("},"); json.put(data); } data.append("]"); editor.putString(prefKey, intDict.size() == 0 ? null : data.toString()); editor.commit(); }

并阅读json字符串

public static SparseIntArray getArrayPref(Context context, String prefKey) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String json = prefs.getString(prefKey, null); SparseIntArray intDict = new SparseIntArray(); if (json != null) { try { JSONArray jsonArray = new JSONArray(json); for (int i = 0; i < jsonArray.length(); i++) { JSONObject item = jsonArray.getJSONObject(i); intDict.put(item.getInt("key"), item.getInt("order")); } } catch (JSONException e) { e.printStackTrace(); } } return intDict; }

并像这样使用:

SparseIntArray myKeyList = new SparseIntArray(); ... //write list saveArrayPref(getApplicationContext(),"MyList", myKeyList); ... //read list myKeyList = getArrayPref(getApplicationContext(), "MyList");

You can use the power of JSON to save in the shared preferences for any type of object

For example SparseIntArray Save items like Json string

public static void saveArrayPref(Context context, String prefKey, SparseIntArray intDict) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = prefs.edit(); JSONArray json = new JSONArray(); StringBuffer data = new StringBuffer().append("["); for(int i = 0; i < intDict.size(); i++) { data.append("{") .append("\"key\": ") .append(intDict.keyAt(i)).append(",") .append("\"order\": ") .append(intDict.valueAt(i)) .append("},"); json.put(data); } data.append("]"); editor.putString(prefKey, intDict.size() == 0 ? null : data.toString()); editor.commit(); }

and read json string

public static SparseIntArray getArrayPref(Context context, String prefKey) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); String json = prefs.getString(prefKey, null); SparseIntArray intDict = new SparseIntArray(); if (json != null) { try { JSONArray jsonArray = new JSONArray(json); for (int i = 0; i < jsonArray.length(); i++) { JSONObject item = jsonArray.getJSONObject(i); intDict.put(item.getInt("key"), item.getInt("order")); } } catch (JSONException e) { e.printStackTrace(); } } return intDict; }

and use like this:

SparseIntArray myKeyList = new SparseIntArray(); ... //write list saveArrayPref(getApplicationContext(),"MyList", myKeyList); ... //read list myKeyList = getArrayPref(getApplicationContext(), "MyList");

更多推荐

本文发布于:2023-04-13 12:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/f23f80d6ab87f569786d5e38d3b782e6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SparseBooleanArray   SharedPreferences   Save

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!