How to Store an Integer Array in Sharedpreferences

How can I store an integer array in SharedPreferences?

You can try to do it this way:

  • Put your integers into a string, delimiting every int by a character, for example a comma, and then save them as a string:

    SharedPreferences prefs = getPreferences(MODE_PRIVATE);
    int[] list = new int[10];
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < list.length; i++) {
    str.append(list[i]).append(",");
    }
    prefs.edit().putString("string", str.toString());
  • Get the string and parse it using StringTokenizer:

    String savedString = prefs.getString("string", "");
    StringTokenizer st = new StringTokenizer(savedString, ",");
    int[] savedList = new int[10];
    for (int i = 0; i < 10; i++) {
    savedList[i] = Integer.parseInt(st.nextToken());
    }

How would I save an Integer array list to shared preferences

You can't write any arrays or arraylists to shared preferences. The closest you can do is write the integers to a comma separated string, write the string, and parse it when you need to read it. This is only appropriate if the size of the array is relatively small. If its large, you need to move away from SharedPreferences to another form of storage such as a file or database.

How do I add an int array into shared preferences?

you can add only primitive values to sharedpreference ......

refer this doc:

http://developer.android.com/reference/android/content/SharedPreferences.html

Save ArrayList to SharedPreferences

After API 11 the SharedPreferences Editor accepts Sets. You could convert your List into a HashSet or something similar and store it like that. When you read it back, convert it into an ArrayList, sort it if needed and you're good to go.

//Retrieve the values
Set<String> set = myScores.getStringSet("key", null);

//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();

You can also serialize your ArrayList and then save/read it to/from SharedPreferences. Below is the solution:

EDIT:

Ok, below is the solution to save ArrayList as a serialized object to SharedPreferences and then read it from SharedPreferences.

Because API supports only storing and retrieving of strings to/from SharedPreferences (after API 11, it's simpler), we have to serialize and de-serialize the ArrayList object which has the list of tasks into a string.

In the addTask() method of the TaskManagerApplication class, we have to get the instance of the shared preference and then store the serialized ArrayList using the putString() method:

public void addTask(Task t) {
if (null == currentTasks) {
currentTasks = new ArrayList<task>();
}
currentTasks.add(t);

// save the task list to preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
try {
editor.putString(TASKS, ObjectSerializer.serialize(currentTasks));
} catch (IOException e) {
e.printStackTrace();
}
editor.commit();
}

Similarly we have to retrieve the list of tasks from the preference in the onCreate() method:

public void onCreate() {
super.onCreate();
if (null == currentTasks) {
currentTasks = new ArrayList<task>();
}

// load tasks from preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);

try {
currentTasks = (ArrayList<task>) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(new ArrayList<task>())));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

You can get the ObjectSerializer class from the Apache Pig project ObjectSerializer.java

How to use SharedPreferences to save more than one Integer values?

For saving in SharedPreferences:

public void putListInt(String key, ArrayList<Integer> intList) {
checkForNullKey(key);
Integer[] myIntList = intList.toArray(new Integer[intList.size()]);
preferences.edit().putString(key, TextUtils.join("‚‗‚", myIntList)).apply();
}

For retrieving from SharedPreferences:

public ArrayList<Integer> getListInt(String key) {
String[] myList = TextUtils.split(preferences.getString(key, ""), "‚‗‚");
ArrayList<String> arrayToList = new ArrayList<String>(Arrays.asList(myList));
ArrayList<Integer> newList = new ArrayList<Integer>();

for (String item : arrayToList)
newList.add(Integer.parseInt(item));

return newList;
}

How can i store an Integer Array in SharedPreferences in Android?

You can store it as a String by transforming it:

Arrays.toString(upVal)

To get it back and convert a String to an Integer array is trivial.



Related Topics



Leave a reply



Submit