Fatal Exception
Fatal Exception

Reputation: 167

how to retrieve int value from Sharedpreferences in Android

I am trying to save and get value from sharedpreference but as far as storing data concern its working good but i'm helpless while retrieving the value.

//set constant

private static final String WALLPAPER_PREF_FILE = "wallpaper";

//method for storing the key-value pair.

public void saveWallpaper(Context context,int wallpaperId) {

       SharedPreferences pref = context.getSharedPreferences(WALLPAPER_PREF_FILE, Activity.MODE_PRIVATE);
        SharedPreferences.Editor  editor = pref.edit();
        editor.putInt("wallpaperId", wallpaperId);
        editor.commit();
     }

//Method to Retrieve value stored in sharedPreference.

 public static int getSavedWallpaper(Context context) {

        SharedPreferences pref = context.getSharedPreferences(
                WALLPAPER_PREF_FILE, Activity.MODE_PRIVATE);
        return pref.getInt("wallpaperId", -1);
    }

Upvotes: 0

Views: 1700

Answers (5)

Suhas Bachewar
Suhas Bachewar

Reputation: 1230

public class TestActivity extends AppCompatActivity {
    private SharedPreferences mSharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        mSharedPreferences = getSharedPreferences("wom_user_preference",Context.MODE_PRIVATE);

        //save actual drawable id in this way.
        int resId1 =  R.drawable.splash_img;       
        saveInt("wallpaerId", resId1);

        int resId = R.drawable.fab_selector;
        int id = loadInt("wallpaerId", resId);
        Log.d("Test", "" + id);

    }

    public void saveInt(String key, int value)
    {
        if(mSharedPreferences==null)
            return;

        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }
    private int loadInt(String key, int defaultValue) {
        int data = mSharedPreferences.getInt(key, defaultValue);
        return data;
    }

}

Upvotes: 1

Vishal Patoliya ツ
Vishal Patoliya ツ

Reputation: 3238

Step 1: Create Common class named SessionManager

public class SessionManager {

  final static String appPrefNames="MyAppInfo";
    public static SharedPreferences myPrefsSession;
    public static SharedPreferences.Editor prefsEditorSession;

    public static void setUserId(Context context, String userId) {

        myPrefsSession = context.getSharedPreferences(appPrefNames, Context.MODE_PRIVATE);
        prefsEditorSesstion = myPrefsSession.edit();
        try {
           prefsEditorSesstion.putString("user_id", userId);

        } catch (Exception e) {
            e.printStackTrace();
        }
        prefsEditorSesstion.commit();
    }


    public static String getUserId(Context c) {
        myPrefsSession = c.getSharedPreferences(appPrefNames, Context.MODE_PRIVATE);
        String userId= myPrefsSession.getString("user_id", "");
        return userId;
    }
}

Step 2 :In Your Activity class

SessionManager sessionManager = new SessionManager();
//Setting UserId by method
sessionManager.setUserId(this,"12");

// get Your user id
String userId = sessionManager.getUserId();

Upvotes: 0

Ravindra Kushwaha
Ravindra Kushwaha

Reputation: 8042

Try these for receiving the value.

public static String getSharedPrefData(Context activity, String key) {
    SharedPreferences prefs = activity.getSharedPreferences(PREFRENCE_NAME,
            Context.MODE_PRIVATE);
    String value = "";
    if (prefs != null && prefs.contains(key)) {
        value = prefs.getInt(key, defaultValue);
    }
    return value;
}

Upvotes: 0

Pitty
Pitty

Reputation: 1977

private int userId;
private static final int PUSH_USER_ID = 0;

Getter Setter Method

public void setUserId(int userId) {
    this.userId = userId;
    SharedPreferences.Editor Editor = pref.edit();
    Editor.putInt(PUSH_USER_ID, userId);
    Editor.apply();
    Log.e(TAG, userId + " successfull");
}

public int getUserId() {
    return userId;
}

//Retriving int value

SharedPreferences pref = new SharedPreferences();
    pref.getUserId()

Upvotes: -1

zmarties
zmarties

Reputation: 4869

Look like you have a typo in the retrieval - wallpaerId. Fix that up and it should work. (Even better, use a constant string in both saving and retrieving to avoid exactly this problem).

Upvotes: 4

Related Questions