我使用此代码在SharedPreference中保存String值,但返回空String!我非常努力地花了很长时间,但我不明白为什么?
sp=getSharedPreferences("sp", Activity.MODE_PRIVATE);
bshare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
sp.edit().putString("a","ali");
sp.edit().commit();
Log.i("sp","z"+sp.getString("a","");
}
});
答案 0 :(得分:0)
尝试这种方式:
SharedPreferences sp = this.getSharedPreferences("sp", Activity.MODE_PRIVATE);
SharedPreferences.Editor aa = sp.edit();
Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
// Add the new value.
myStrings.add("Another string");
// Save the list.
aa.putStringSet("myStrings", myStrings);
aa.commit();