Remove
the key for HashSet
in SharedPreferences
, commit
then add new value.
SharedPreferences ss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
ss = getSharedPreferences("db", 0);
fun();
}
void fun() {
Log.i("chauster", "1.set = "+ss.getStringSet("set", new HashSet()));
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Set hs = ss.getStringSet("set", new HashSet());
hs.add(String.valueOf(hs.size()+1));
Log.i(TAG, "list: " + hs.toString());
Editor edit = ss.edit();
edit.remove("set");
edit.commit();
edit.putStringSet("set", hs);
Log.i(TAG, "saved: " + edit.commit());
Log.i("chauster", "2.set = "+ss.getStringSet("set", new HashSet()));
}
});
}