关于SharedPreferences中无法改变Set的问题。

    关于SharedPreferences中getStringSet无法改变Set的问题

     最近在做android项目的时候,遇到一个很常见的需求,就是在动态的将历史输入记录,在autocompletetextview中输入框自动提示补全,由于是动态的,故补全内容应该动态的从数据库中,或SharedPreferences中获取。然而就自然而然的想起了SharedPreferences中存在的putStringSet方法,但是在实际使用中遇到一个奇怪的问题,我每次成功输入之后都会讲输入记录存入Set中,更确切的说应该是追加入之前的Set集合中,那就免不了在调用putStringSet之前,需要先在初始化的时候getStringSet出我之前已存在的历史Set。之后,我就犯了如下错误

     HashSet<String> set=gettStringSet("History");

     set.add("ABC");//例如我输入了ABC

     set.add("123");

     ...

     ...

      set.add("789");

     putStringSet("History",set);

     我以为这样写就可以达到更新了SharedPreferences中,Key等于History的set集合。

  

public abstract SharedPreferences.Editor putStringSet (String key, Set<String> values)

Added in API level 11
Set a set of String values in the preferences editor, to be written back once commit() is called.

Parameters
key	The name of the preference to modify.
values	The new values for the preference.
Returns
Returns a reference to the same Editor object, so you can chain put calls together.


但是事实它并没有成功的保存到Set中。我通过查看/data/data/your_package目录下  SharedPreferences_自定义的名字.xml文件中,可以看到,并没有保存到之前我输入的ABC,123, ......789。 实在郁闷之后,翻看API。 我们可以清楚地看到API里提示,我们不能直接修改通过getStringSet方法调用的那个实例,即我们不能像刚才那样,直接在得到set之后,直接新增或删除set中数据,之后再put回去。

public abstract Set<String> getStringSet (String key, Set<String> defValues)

Added in API level 11
Retrieve a set of String values from the preferences.

Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

Parameters
key	The name of the preference to retrieve.
defValues	Values to return if this preference does not exist.
Returns
Returns the preference values if they exist, or defValues. Throws ClassCastException if there is a preference with this name that is not a Set.
Throws
ClassCastException	


 

所以介于以上方法,可以通过这样的方式更新Set

HashSet<String> set=gettStringSet("History");

     set.add("ABC");//例如我输入了ABC

     set.add("123");

     ...

     ...

      set.add("789");

 

HashSet<String> tepSet =new Hash<String>();

Iterator<String> iterator=set.iterator();

  if(iterator.hasNext()){

     tepSet.add(iterator.next());

   }   

     putStringSet("History",tepSet);

即可完成更新在sp中gengxinSet

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在SharedPreferences实现注销账号的步骤如下: 1. 获取SharedPreferences对象。 2. 使用SharedPreferences.Editor对象将存储的用户数据清除。 3. 调用apply()方法提交修改。 4. 导航回到登录页面或者清除已保存的登录状态。 示例代码如下: ``` // 获取SharedPreferences对象 SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE); // 使用SharedPreferences.Editor对象将存储的用户数据清除 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.clear(); // 调用apply()方法提交修改 editor.apply(); // 导航回到登录页面或者清除已保存的登录状态 Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); finish(); ``` 以上代码清除了名为"user_info"的SharedPreferences对象存储的所有数据,并且导航回到了登录页面。 ### 回答2: 在SharedPreferences实现注销账号可以通过以下步骤完成: 首先,我们需要在SharedPreferences存储用户信息,例如用户名、密码等。可以使用SharedPreferences的edit()方法获取一个Editor对象,然后使用putString()方法将用户信息存储在SharedPreferences。 当用户需要注销账号时,我们可以直接清除SharedPreferences存储的用户信息。可以使用Editor对象的remove()方法来删除指定的键值对,然后使用commit()方法提交更改。 以下是一段示例代码: ```java // 存储用户信息 SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("username", "test"); editor.putString("password", "123456"); editor.commit(); // 注销账号 SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.remove("username"); editor.remove("password"); editor.commit(); ``` 通过以上代码,我们可以成功地在SharedPreferences实现注销账号。当用户注销账号时,相关的用户信息将被从SharedPreferences删除,从而达到实现注销账号的目的。 ### 回答3: 在SharedPreferences实现注销账号的过程比较简单。首先,我们需要知道SharedPreferences是一个存储键值对数据的文件。所以,要实现注销账号,我们可以选择清空SharedPreferences存储的用户信息。 首先,让我们假设在SharedPreferences文件,我们存储了用户的账号信息,包括用户名和密码等。我们需要通过SharedPreferences对象来获取和编辑这些信息。 首先,我们需要获取SharedPreferences对象,可以使用以下代码: SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE); 接下来,我们可以使用SharedPreferences对象获取一个Editor对象,用于编辑SharedPreferences的数据: SharedPreferences.Editor editor = sharedPreferences.edit(); 接下来,我们可以使用Editor对象的remove()方法移除账号信息。例如,如果我们存储了键名为"username"的用户名和键名为"password"的密码,我们可以使用以下代码: editor.remove("username"); editor.remove("password"); 最后,我们需要使用commit()方法来提交这些修改: editor.commit(); 在这个过程,我们只需要将"username"和"password"替换为实际的键名即可。 这样,我们就成功将SharedPreferences存储的用户信息清除了,实现了注销账号的功能。当用户重新登录时,SharedPreferences将不再包含任何用户信息,需要重新输入用户名和密码来登录。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值