[英]Trying to update shared preference from a Fragment Activity
I'm building a custom ringtone selector for my app. 我正在为我的应用构建自定义铃声选择器。 The class is called SoundPicker
and it extends FragmentActivity
. 该类称为SoundPicker
,它扩展了FragmentActivity
。 What I'm trying to do is save the selected ringtone to my app's settings file. 我正在尝试将选定的铃声保存到应用程序的设置文件中。
Here is my code: 这是我的代码:
String selectedResource = "blablabla";
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet("notifications_new_message", selectedResource);
editor.commit();
The problem is that I get an error, refering to the new string I'm trying to set - selectedResource
问题是,我得到一个错误,指的新字符串我正在尝试设置- selectedResource
That's the error I get: 那就是我得到的错误:
Wrong 2nd argument type. 第二个参数类型错误。 Found: 'java.lang.String', required: 'java.util.Set' 找到:“ java.lang.String”,必需:“ java.util.Set”
putStringSet (String, java.util.Set) in Editor cannot be applied to (String, java.lang.String) 编辑器中的putStringSet(String,java.util.Set)不能应用于(String,java.lang.String)
What am I doing wrong here? 我在这里做错了什么? Thank you! 谢谢!
Please be careful, putString and putStringSet is different method, so use putString instead 请注意,putString和putStringSet是不同的方法,因此请改用putString
editor.putString("notifications_new_message", selectedResource); editor.putString(“ notifications_new_message”,selectedResource);
You are trying to set string but putStringSet accept 2nd argument of type set 您正在尝试设置字符串,但是putStringSet接受类型为set的第二个参数
use putString to store string value or create string set and use putStringSet 使用putString存储字符串值或创建字符串集并使用putStringSet
//To store string value
String selectedResource = "blablabla";
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("notifications_new_message", selectedResource);
editor.commit();
//To Store string set
Set<String> hs = ss.getStringSet("set", new HashSet<String>());
String selectedResource = "blablabla";
hs.add(selectedResource);
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet("notifications_new_message", hs);
editor.commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.