1

I have created the local data like below code

public class LocalDataSource {

private SharedPreferences mSharedPreferences;
private static LocalDataSource sLocalRepository;

private LocalDataSource() {
    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.getApp());
}

public static LocalDataSource getInstance() {
    if (sLocalRepository == null) {
        sLocalRepository = new LocalDataSource();
    }
    return sLocalRepository;
}

public void saveResponse(Context ctx, String object, String key) {
    mSharedPreferences.edit().putString(key, object).apply();
}

public String getResposne(String key) {
    return mSharedPreferences.getString(key, "");
}

}

and then I have save the object as String in sharedpreferences like below code

@Override
        public void onSuccess(LoadLookUpResponse body) {
            super.onSuccess(body);

            if (body.responseCode != CommonResponse.Code.SUCCESS) {

                PopupErrorDialog.newInstance(body.responseMessage.header, body.responseMessage.message, body.responseMessage.btnText1, null, null, null).show(getSupportFragmentManager(), "popup_error");

            } else {
                mData = body.data;

                LocalDataSource.getInstance().saveResponse(getApplicationContext(), mData.toString(), "data");


            }
        }

Now I can get this Object String will return when I call this

LocalDataSource.getInstance().getResposne("data");

Now, How can I get this as my Response object (LoadLookUpResponse.Data) to access in my specific classes ? Because, It's returning the String. But My responses are String and arrays.

Thanks in advance.

AngelJanniee
  • 613
  • 1
  • 11
  • 30

3 Answers3

1

you can convert value in class to String.

save

 sharedPreferences.putString("data", new GsonBuilder().create().toJson(Resposne));

load

String resposneString = sharedPreferences.getString("data", "");
        if (!userString.isEmpty()) {
            Constants.tutorialConfig = new GsonBuilder()
                    .serializeNulls()
                    .create()
                    .fromJson(resposneString
                            , DataResponce.class);
        }
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
1
public class PreferenceUtil {
    public static void saveToPreferences(Context context, String preferenceName, String preferenceValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(preferenceName, preferenceValue);
        editor.apply();
    }

    public static void saveToPreferences(Context context, String preferenceName, boolean preferenceValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(preferenceName, preferenceValue);
        editor.apply();
    }

    public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        return sharedPreferences.getString(preferenceName, defaultValue);
    }

    public static boolean readFromPreferences(Context context, String preferenceName, boolean defaultValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        return sharedPreferences.getBoolean(preferenceName, defaultValue);
    }
}

// Use this class to write preference and read preference
// Create instance of PreferenceUtil class in your Activity
private PreferenceUtil mPreferenceUtil;
mPreferenceUtil.saveToPreferences(contex, prefKey, prefValue);
Athar Iqbal
  • 458
  • 3
  • 12
0

You can save this in form of JSON string in SharedPreferences. Then while fetching you'll be able to get it in your class.

Neha
  • 76
  • 11
  • I didn't get. Please elaborate more with example – AngelJanniee Jul 26 '17 at 12:33
  • Ok you can cast your JSON response to GSON and for that you can use the complete class. You can take reference of this link https://stackoverflow.com/questions/22753719/how-to-parse-json-parsing-using-gson-in-android – Neha Jul 26 '17 at 12:35