How to retrieve a list of data from SharedPreferences

I am trying to retrieve data from sharedPreferences:

    public static void loadArray(Context mContext) {
    SharedPreferences mSharedPreference1 = PreferenceManager.getDefaultSharedPreferences(mContext);
    WorkList.clear();
    int size = mSharedPreference1.getInt("Status_size", 0);
    for (int i = 0; i < size; i++) {
        WorkList.add(mSharedPreference1.getString("Status_" + i, null));
    }
}

i got this code but don't know how to call this method. I try to call this method in the same non-activity class.

Upvotes: 2

Views: 67

Answers (1)

PotatoManager
PotatoManager

Reputation: 1775

Create an app Singleton Class by following this link

Then you can call the method by loadArray(AppSingleton.getInstance().getContext())

Once you are a bit comfortable and have understood the concept switch to using Dagger2 to handle singletons

Upvotes: 1

Related Questions