开发者

Android shared Prefrence problem

开发者 https://www.devze.com 2023-01-24 15:41 出处:网络
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..

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消