Android shared Prefrence problem
HI all,
i want to ask can i save an array in shared prefrence(Default Shared Prefrence)... if yes then pls help me to save the array in shared prefren开发者_StackOverflow中文版ce.. Any code would b great if available.
thanks in advance.
You could write each element of your array using a different key... something like this (for Strings):
void storeArrayToPrefs(SharedPreferences prefs, String a[]) {
SharedPreferences.Editor editor = prefs.edit();
for (int i=0 ; i<a.length ; i++) {
editor.putString("key" + i, a[i]);
}
editor.commit();
}
I believe you can only get primitive data types from the SharedPreference class. See this on the dev guide: http://developer.android.com/intl/de/reference/android/content/SharedPreferences.html GetBoolean
, GetInt
, etc.
Depending on what type of data you have, you may want to consider using a SQLite database. See here for a tutorial.
All the primitive data types like booleans, floats, ints, longs, and strings are supported.
You can store all the values of the array in Key value format using a loop and later if you want to retrieve make use of HashMap.
精彩评论