-2

I am trying to use SharedPreferences as in the answer given here

I am trying to read int value from it like below code

int theme= Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");

but its giving me error like below

readInteger() in Preferences cannot be applied to:
Expected : Actual
Parameter : Arguments 

Preference Class

public class Preferences {
    public static final String PREF_NAME = "your preferences name";

    public static final int MODE = Context.MODE_PRIVATE;

    public static final int themeNew = 1;
    public static final String USER_NAME = "USER_NAME";

    public static final String NAME = "NAME";
    public static final String EMAIL = "EMAIL";
    public static final String PHONE = "PHONE";
    public static final String address = "address";

    public static void writeBoolean(Context context, String key, boolean value) {
        getEditor(context).putBoolean(key, value).commit();
    }

    public static boolean readBoolean(Context context, String key,
                                      boolean defValue) {
        return getPreferences(context).getBoolean(key, defValue);
    }

    public static void writeInteger(Context context, String key, int value) {
        getEditor(context).putInt(key, value).commit();

    }

    public static int readInteger(Context context, String key, int defValue) {
        return getPreferences(context).getInt(key, defValue);
    }

    public static void writeString(Context context, String key, String value) {
        getEditor(context).putString(key, value).commit();

    }

    public static String readString(Context context, String key, String defValue) {
        return getPreferences(context).getString(key, defValue);
    }

    public static void writeFloat(Context context, String key, float value) {
        getEditor(context).putFloat(key, value).commit();
    }

    public static float readFloat(Context context, String key, float defValue) {
        return getPreferences(context).getFloat(key, defValue);
    }

    public static void writeLong(Context context, String key, long value) {
        getEditor(context).putLong(key, value).commit();
    }

    public static long readLong(Context context, String key, long defValue) {
        return getPreferences(context).getLong(key, defValue);
    }

    public static SharedPreferences getPreferences(Context context) {
        return context.getSharedPreferences(PREF_NAME, MODE);
    }

    public static SharedPreferences.Editor getEditor(Context context) {
        return getPreferences(context).edit();
    }

}

Why?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Priya
  • 1,602
  • 4
  • 22
  • 37
  • Please provide a **complete** code example. Is this line in an `onCreate()` method? If so, show that **in code**. Also show the enclosing class. Be careful to not include extra code that is not pertinent to your question. Check out [mcve] for more tips on creating a good code example. – Code-Apprentice Oct 15 '17 at 20:54
  • 1
    Shared Preference or just a preferences! – Xenolion Oct 15 '17 at 20:54
  • I think problem is you are trying to read an integer value but you are setting default value as "" String instead of an int. You should share what readInteger() method does for exact answer. – Thracian Oct 15 '17 at 20:56
  • @FatihOzcan I am setting int as value. Please check code added – Priya Oct 15 '17 at 20:58
  • @Code-Apprentice I have added code – Priya Oct 15 '17 at 20:59
  • 1
    No you are not setting default value as int. getPreferences(context).getInt(key, defValue). defValue is String. "" is not an int – Thracian Oct 15 '17 at 21:00

3 Answers3

0

You should use:

String KEY_VALUE = "MY_KEY";
int not_found = -1;
int theme = getPreferences(MODE_PRIVATE).getInt(KEY_VALUE,not_found);

Where MODE_PRIVATE means the Operating mode that you're using (MODE_PRIVATE is the default mode).

in your case, not_found should be the default_theme.

I think that your code isn't working because you're sending a string value when an int is being expected(an int is being expected at your_default value but you're using a string). Is your code building with success?

ps: We can provide a better help if you provide your classes.

Thiago Souto
  • 761
  • 5
  • 13
0

The problem is that the static method readInteger() does not have the last argument as String but as int so please change this line also themeNew should also be String key that you use to retrieve your Integer theme:

    int theme= Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");

To be this:

    int theme= Preferences.readInteger(getApplicationContext(), "my_theme",0);

Where 0 is the default value if your preference is not found! Happy Coding!

Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • its giving me error called wrong 2nd argument type. Found int required java.lang.String – Priya Oct 15 '17 at 21:08
  • 1
    Yes the second argument should be a string name to the Key. I have updated the answer to modify that! @Priya – Xenolion Oct 15 '17 at 21:16
0

Your problem is setting String value for int value.

 public static int readInteger(Context context, String key, int defValue) {
        return getPreferences(context).getInt(key, defValue);
 }

This method requires defValue to be an int but Preferences.readInteger(getApplicationContext(), Preferences.themeNew,"");, "" is not an int value, it's a String with zero length

Thracian
  • 43,021
  • 16
  • 133
  • 222