添加到有关的答案
您提供的解决方案不会附加新值。它会覆盖它们
以下是一组可用于附加到现有共享首选项集的方法。
public void appendSharedPreference(SharedPreferences sharedPreferences,String key, Object o) {
SharedPreferences.Editor e = sharedPreferences.edit();
for (Map.Entry<String,?> entry: sharedPreferences.getAll().entrySet()) {
sharedPrefPut(e,entry.getKey(),entry.getValue());
}
sharedPrefPut(e,key,o);
e.commit();
}
public void sharedPrefPut(SharedPreferences.Editor e, String key, Object o) {
if (o instanceof Boolean) e.putBoolean(key,(Boolean) o);
if (o instanceof String) e.putString(key,(String) o);
if (o instanceof Integer) e.putInt(key,(Integer) o);
if (o instanceof Long) e.putLong(key,(Long) o);
if (o instanceof Float) e.putFloat(key,(Float) o);
if (o instanceof Set) e.putStringSet(key,(Set<String>) o);
}
在 Activity 中使用它的一个例子是:-
SharedPreferences.Editor e1 = this.getSharedPreferences("test",0).edit();
e1.putInt("KEY1",100);
e1.putString("KEY2","MYDATA");
e1.commit();
for (Map.Entry<String,?> e: this.getSharedPreferences("test",0).getAll().entrySet()) {
Log.d("SPINITIAL","Key is " + e.getKey() + " Value is " + e.getValue());
}
appendSharedPreference(this.getSharedPreferences("test",0),"KEYAPPENDANOTHER","SomeOtherData");
for (Map.Entry<String,?> e: this.getSharedPreferences("test",0).getAll().entrySet()) {
Log.d("SPEND","Key is " + e.getKey() + " Value is " + e.getValue());
}
以上产生的日志
前
D/SPINITIAL:键是 KEY2 值是 MYDATA
D/SPINITIAL:键为 KEY1 值为 100
后
D/SPEND:键是 KEY2 值是 MYDATA
D/SPEND:键为 KEY1 值为 100
D/SPEND:键是 KEYAPPENDANOTHER 值是 SomeOtherData
当然,过度使用/滥用 SharedPreferences 可能会对性能产生影响
编辑关于编辑
可以使用一种方法(细微的变化),例如
public void replaceSharedPreference(SharedPreferences sharedPreferences, String key, Object o) {
SharedPreferences.Editor e = sharedPreferences.edit();
for (Map.Entry<String, ?> entry: sharedPreferences.getAll().entrySet()) {
if (entry.getKey().equals(key)) {
sharedPrefPut(e,entry.getKey(),o);
} else {
sharedPrefPut(e,entry.getKey(),entry.getValue());
}
}
}
因此,它不会追加新的,而是替换现有的。必须构建传递的对象,并且根据您的要求,它需要提取并附加现有对象。
根据您的要求的示例可能是:-
ArrayList<String> array = new ArrayList<>();
array.add("ONE");array.add("TWO");array.add("THREE");
HashSet<String> set = new HashSet<>(array);
SharedPreferences.Editor e1 = this.getSharedPreferences("test",0).edit();
e1.putInt("KEY1",100);
e1.putString("KEY2","MYDATA");
e1.putStringSet("KEY3",set);
e1.commit();
for (Map.Entry<String,?> e: this.getSharedPreferences("test",0).getAll().entrySet()) {
Log.d("SPINITIAL","Key is " + e.getKey() + " Value is " + e.getValue());
}
//appendSharedPreference(this.getSharedPreferences("test",0),"KEYAPPENDANOTHER","SomeOtherData");
ArrayList<String> appendset = new ArrayList<>();
appendset.add("FOUR");
appendset.add("FIVE");
appendset.add("SIX");
HashSet<String> oldset = null;
if ((oldset = (HashSet<String>) this.getSharedPreferences("test",0).getStringSet("KEY3",null)) != null) {
oldset.addAll(appendset);
replaceSharedPreference(this.getSharedPreferences("test",0),"KEY3",oldset);
}
for (Map.Entry<String,?> e: this.getSharedPreferences("test",0).getAll().entrySet()) {
Log.d("SPEND","Key is " + e.getKey() + " Value is " + e.getValue());
}
如果订单有问题,请注意
它不保证集合的迭代顺序;特别是,它不保证订单会随着时间的推移保持不变。
类 HashSet