使用putBoolean更改字符串值在android中返回一个数值?

时间:2016-05-04 14:28:16

标签: android boolean sharedpreferences

我有字符串字段<string name="categegoriesStatus">true</string>

现在在settingsActivity中我正在更改其在首选项单击上的值。

final SharedPreferences sharedpreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.putBoolean(getResources().getString(R.string.categegoriesStatus), false );
                        editor.apply();

但它并没有将其更改为false,而是将其更改为某个数值。而且我没有得到我想要的结果。

3 个答案:

答案 0 :(得分:1)

您有字符串,但您希望保存布尔值。然后,你应该这样:

  boolean result = getResources().getString(R.string.categegoriesStatus).equals("true");
  editor.putBoolean(result, false );
祝你好运!

答案 1 :(得分:1)

您错误地使用了Sharedpreferences。

在语句editor.putBoolean(getResources()。getString(R.string.categegoriesStatus),false); 你正在插入editor.putBoolean(&#34; true&#34;,false);这不是你期望它做的。

存储在共享首选项中的信息应采用键值格式。

从以下链接阅读android文档:http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

答案 2 :(得分:0)

共享首选项仅保存/检索键值对。它不会更改您的字符串资源值。一旦声明了字符串资源字段,就无法在运行时更改它的值。请参阅this answer

相关问题