2017-07-26 3 views
1

J'ai créé les données locales comme ci-dessous le codeComment puis-je obtenir ceci comme objet de réponse de SharedPreference?

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, ""); 
} 

} 

puis j'enregistrer l'objet sous forme de chaîne dans sharedpreferences comme ci-dessous le 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"); 


      } 
     } 

Maintenant, je peux obtenir cette chaîne de l'objet sera de retour quand je appeler cette

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

maintenant, Comment puis-je obtenir ce que mon objet Response (LoadLookUpResponse.Data) d'accéder à mes cours spécifiques? Parce que, c'est le retour de la chaîne. Mais Mes réponses sont Chaîne et tableaux.

Merci d'avance.

Répondre

1

vous pouvez convertir la valeur de la classe en chaîne.

sauver

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

charge

String resposneString = sharedPreferences.getString("data", ""); 
     if (!userString.isEmpty()) { 
      Constants.tutorialConfig = new GsonBuilder() 
        .serializeNulls() 
        .create() 
        .fromJson(resposneString 
          , DataResponce.class); 
     } 
0

Vous pouvez enregistrer ceci sous forme de chaîne JSON dans SharedPreferences. Ensuite, tout en récupérant vous serez en mesure de l'obtenir dans votre classe.

+0

Je n'ai pas. S'il vous plaît élaborer plus avec l'exemple – AngelJanniee

+0

Ok, vous pouvez diffuser votre réponse JSON à GSON et pour cela, vous pouvez utiliser la classe complète. Vous pouvez prendre référence de ce lien https://stackoverflow.com/questions/22753719/how-to-parse-json-parsing-using-gson-in-android – Neha

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);