Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Created October 29, 2017 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Binary-Finery/c89c6bc76faee5e8b2e73dab962484bb to your computer and use it in GitHub Desktop.
Save Binary-Finery/c89c6bc76faee5e8b2e73dab962484bb to your computer and use it in GitHub Desktop.
utility class for saving and retrieving int array to/from shared preferences
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class Utils {
private static final String PREFS_KEY = "numbers";
static int[] getNumbersFromSharedPreference(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String p = sharedPreferences.getString(PREFS_KEY, "88,21,6,17,65,59,11,90");
String[] spl = p.split(",");
int[] numbers = new int[spl.length];
for (int i = 0; i < spl.length; i++) numbers[i] = Integer.parseInt(spl[i]);
return numbers;
}
static void saveNumbersToSharedPreferences(Context context, int[] numbers) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
String strArr = "";
for (int i : numbers) strArr += String.valueOf(i).concat(",");
editor.putString(PREFS_KEY, strArr).apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment