-1

I have string field <string name="categegoriesStatus">true</string>

Now inside settingsActivity I am changing its value on preference click.

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

But it is not changing it to false, but change it to some numeric value. And I don't get my desired result.

Gini
  • 1
  • 4

3 Answers3

1

You have a string, but you want to save a boolean. Then, you should be this:

  boolean result = getResources().getString(R.string.categegoriesStatus).equals("true");
  editor.putBoolean(result, false );

Good luck!

Jose Angel Maneiro
  • 1,226
  • 9
  • 20
  • I got 'Wrong first argument type. Boolean required' in the 'result' variable. I did this : editor.putBoolean(String.valueOf(result), false); But it won't work. – Gini May 04 '16 at 15:01
  • You can try cast result to Boolean object. Maybe `(Boolean) result`? or `Boolean result = getResources().getString(R.string.categegoriesStatus).equals("true");` – Jose Angel Maneiro May 04 '16 at 15:28
1

You are using Sharedpreferences wrongly.

in the statement editor.putBoolean(getResources().getString(R.string.categegoriesStatus), false ); you are inserting editor.putBoolean("true", false); which is not what you are expecting it to do.

Information stored in shared preferences should be in key value format.

Read the android documentation from this link: http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

Avinash4551
  • 220
  • 1
  • 9
0

Shared Preferences only saves/retrieve key value pairs. It does not change your string resource values. Once you have declared a string resource field, you cannot change it's value at runtime. Please refer to this answer.

Community
  • 1
  • 1
Natan
  • 1,867
  • 13
  • 24