Android的共享preferences更新不起作用 [英] Android SharedPreferences update does not work

查看:201
本文介绍了Android的共享preferences更新不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,这个问题已在多个线程被处理,但我想不通这一个。
所以我设置的共享preference是这样的:

I know, this issue has been dealt with in many threads, but I cannot figure out this one. So I set a shared preference like this:

SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet(spinnerName, myValueSet  );
editor.apply();

我读了preferences是这样的:

I read the preferences like this:

SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE);
Set<String> spinnerValuesSet = null;
spinnerValuesSet = prefs.getStringSet(spinnerName,null );

一切正常,除了同时本次活动即运行我的变化是显而易见的 - 我显示从共享preferences值,允许用户删除或添加,然后更新的ListView。这工作,但我重新启动应用程序后,我得到的初始值。
例如,这是我的方法从列表中删除一个值,更新共享preferences的价值观和更新的ListView

Everything works, except for my changes are visible while this activity runs i.e. - I display the values from the SharedPreferences, allow the user to delete or add and then update the ListView. This works, but after I restart the application, I get the initial values. This for example is my method to delete one value from the list, update the values in SharedPreferences and update the ListView

Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View arg0) {
    SharedPreferences prefs =  MainActivity.this.getPreferences(MODE_PRIVATE);
    Set<String> spinnerValuesSet = prefs.getStringSet(spinnerName,null );
    for (String s : spinnerValuesSet)
    {
         if(s == currentSelectedItemString)
         {
             spinnerValuesSet.remove(s);
             SharedPreferences.Editor editor = prefs.edit();
             editor.putStringSet(spinnerName, spinnerValuesSet );
                 editor.apply();
             break;
         }
    }
 updateListValues();

}
});

这是更新的ListView方法:

And this is the method that updates the ListView:

 private void updateListValues()
 {
   SharedPreferences prefs = MainActivity.this.getPreferences(MODE_PRIVATE);
   Set<String> spinnerValuesSet = prefs.getStringSet(spinnerName,null );
   if(spinnerValuesSet.size() > 0) 
    {
        names = new ArrayList<String>();
        names.clear();
        int k=0;
        for (String s : spinnerValuesSet) {
             names.add(k, s);
             k++;
        }
        namesAA = new ArrayAdapter<String> (  this, android.R.layout.simple_list_item_activated_1, names );
        myList.setAdapter(namesAA);
   }

}

任何帮助很多AP preciated。

Any help is much appreciated.

推荐答案

通过共享preferences的各种get方法返回的对象应被视为不可变的。见<一href=\"http://developer.android.com/reference/android/content/Shared$p$pferences.html\">Shared$p$pferences类概述,以供参考。

The Objects returned by the various get methods of SharedPreferences should be treated as immutable. See SharedPreferences Class Overview for reference.

您必须调用删除(字符串)通过共享preferences.Editor 按返回共享preferences.edit(),而不是直接对共享preferences.getStringSet(字符串,请设置&LT;弦乐&GT;)。

You must call remove(String) through the SharedPreferences.Editor returned by SharedPreferences.edit() rather than directly on the Set returned by SharedPreferences.getStringSet(String, Set<String>).

您将需要建立一个新的设置,每次包含更新的内容字符串,因为你有,当你想更新其内容删除共享preferences设置项。

You will need to construct a new Set of Strings containing the updated content each time since you have to remove the Set entry from SharedPreferences when you want to update its content.

这篇关于Android的共享preferences更新不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆