我正在尝试使用 SharedPreferences.putstringset
将用户名放入字符串集中,问题是我想获取用户输入的每个名称(值)并将其添加到数组列表中,然后打印每个值。
我尝试了 pref.getstringset
但我不知道该怎么做。
SharedPreferences.Editor editor=pref.edit();
editor.putStringSet("user names",usernames.getText().toString());
editor.commit();
最佳答案
像这样将用户名存储在一个集合中:-
Set<String> userNames = new HashSet<>();
userNames.add(usernames.getText().toString())
然后将
设置在共享首选项中:-
editor.putStringSet("user names",userNames);
editor.commit();
然后最终使用默认值
获取它并打印
它:-
for(String name : pref.getStringSet("user names", new HashSet<>()))
System.out.println(name);
关于java - 如何将 sharedpref.getstringset 的每个值添加到数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57494491/