Created
October 29, 2017 09:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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