如何在SharedPreferences中存储键值对?(How to store key value pairs in SharedPreferences?)

编程入门 行业动态 更新时间:2024-10-15 12:36:40
如何在SharedPreferences中存储键值对?(How to store key value pairs in SharedPreferences?)

我正在开发一个随机生成密码的应用程序。 我正在添加“保存”功能,以便用户可以将其密码保存在SharedPreferences 。 (这不是你应该用你的密码做的)

我有一个名为PasswordActivity的活动,在该活动中我显示保存的密码。

这是我保存密码的方式:我提示用户输入密码的名称/密钥,以便他/她以后可以识别。 然后我在实用程序类中使用以下方法将密钥和密码保存在SharedPreferences中:

public static int savePassword (Context c, String passwordKey, String passwordValue) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (c); Set<String> keys = prefs.getStringSet (PASSWORD_KEYS, new HashSet<String> ()); Set<String> values = prefs.getStringSet (PASSWORD_VALUES, new HashSet<String> ()); SharedPreferences.Editor editor = prefs.edit (); boolean duplicateKey = !keys.add (passwordKey); boolean duplicateValue = !values.add (passwordValue); if (duplicateKey) return KEY_DUPLICATE; if (duplicateValue) return VALUE_DUPLICATE; editor.putStringSet (PASSWORD_KEYS, keys). putStringSet (PASSWORD_VALUES, values).apply (); return SUCCESS; }

我写了另一种获取密码及其名称的方法。 您可能已经猜到了该方法返回HashMap<String, String> 。

public static HashMap<String, String> getPasswords (Context c) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (c); Set<String> keys = prefs.getStringSet (PASSWORD_KEYS, new HashSet<String> ()); Set<String> values = prefs.getStringSet (PASSWORD_VALUES, new HashSet<String> ()); ArrayList<String> keysList = new ArrayList<> (); ArrayList<String> valuesList = new ArrayList<> (); for (String key : keys) { keysList.add (key); } for (String value : values) { valuesList.add (value); } HashMap<String, String> map = new HashMap<> (); for (int i = 0 ; i < keys.size () ; i++) { map.put (keysList.get (i), valuesList.get (i)); } return map; }

我承认代码非常混乱。 我首先得到两组中的东西并将它们转换为ArrayList并将数组列表中的东西添加到地图并返回。

唷! 这是很多工作!

现在,当用户生成密码并保存密码时,这两组包含正确的内容:(这些是假数据)

keys: key1 values: value1

这一切都很好。 但是当我生成另一个密码并保存它时,这两组变得混乱:

keys: key1 key2 values: value2 value1

现在,当我调用getPasswords方法时,它将返回

{key1, value2}, {key2, value1}

哪个不对。 看完文档后,我发现:

它不保证集合的迭代顺序; 特别是,它不保证订单会随着时间的推移保持不变。

我猜是造成错误结果的原因。

所以我想知道是否有另一种方法来存储这些密码及其名称?

I am developing an app that generates passwords randomly. I am adding a "save" feature so that the user can save their passwords in SharedPreferences. (which is not what you are supposed to do with your passwords)

I have an activity called PasswordActivity and in that activity I display the saved passwords.

This is how I save a password: I prompt the user to enter a name/key thingy for the password so that he/she can identify it later. And then I save the key and the password in the SharedPreferences using the following method in a utility class:

public static int savePassword (Context c, String passwordKey, String passwordValue) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (c); Set<String> keys = prefs.getStringSet (PASSWORD_KEYS, new HashSet<String> ()); Set<String> values = prefs.getStringSet (PASSWORD_VALUES, new HashSet<String> ()); SharedPreferences.Editor editor = prefs.edit (); boolean duplicateKey = !keys.add (passwordKey); boolean duplicateValue = !values.add (passwordValue); if (duplicateKey) return KEY_DUPLICATE; if (duplicateValue) return VALUE_DUPLICATE; editor.putStringSet (PASSWORD_KEYS, keys). putStringSet (PASSWORD_VALUES, values).apply (); return SUCCESS; }

And I wrote another method to get the passwords and their names. The method returns a HashMap<String, String> as you might have guessed.

public static HashMap<String, String> getPasswords (Context c) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (c); Set<String> keys = prefs.getStringSet (PASSWORD_KEYS, new HashSet<String> ()); Set<String> values = prefs.getStringSet (PASSWORD_VALUES, new HashSet<String> ()); ArrayList<String> keysList = new ArrayList<> (); ArrayList<String> valuesList = new ArrayList<> (); for (String key : keys) { keysList.add (key); } for (String value : values) { valuesList.add (value); } HashMap<String, String> map = new HashMap<> (); for (int i = 0 ; i < keys.size () ; i++) { map.put (keysList.get (i), valuesList.get (i)); } return map; }

I admit that the code is pretty messy. I first get the things in the two sets and turn them into ArrayLists and add the stuff in the array list to the map and return.

Phew! That was a lot of work!

Now when the user generates a password and saves it, the two sets contain the right things: (These are fake data)

keys: key1 values: value1

That's all good. But when I generate another password and save it, the two sets become messy:

keys: key1 key2 values: value2 value1

Now when I call the getPasswords method, it will return

{key1, value2}, {key2, value1}

which is incorrect. After looking at the docs, I found out that:

It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

which I guess is the reason that makes the incorrect results.

So I was wondering is there an alternative way to store these passwords and their names?

最满意答案

当您存储多个名称和密码时,我建议使用本地SQLite数据库。 SharedPreferences用于单个值,而不是值集。

如果您无法设置整个SQLiteOpenHelper和所有原始SQL代码,您可以查看一些ORM帮助程序,例如SugarOrm(我目前使用它,它的EXTREMELY易于设置和使用)

As you're storing multiple names & passwords, i would suggest a local SQLite database. SharedPreferences are meant for single values, not sets of values.

If you can't be bothered setting up the entire SQLiteOpenHelper and all that raw SQL code, you could take a look at some ORM helpers such as SugarOrm (i currently use this, its EXTREMELY easy to set up and use)

更多推荐

本文发布于:2023-08-05 20:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键值   如何在   SharedPreferences   pairs   store

发布评论

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

>www.elefans.com

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