How to store array values of type String[] and BigInteger[] into shared preference inside for loop

坚强是说给别人听的谎言 提交于 2019-12-13 06:51:29

问题


I am receiving the following array values in a for loop:

String[] array1 = new String[""];
BigInteger array2 = new BigInteger[10];

for (int i = 0; i <= count; i++) {
array1[i] //of type string array
array2[i] //of type bigint array

//Now inside same loop i want to store and retrieve those values of array
from shared preferences. Can someone tell me how to store values of array
into preference which are of type String[] and BigInteger[] 

}

回答1:


If you want to store whole array data in shared preferences then you need to take shared preferences key array as same size of your array.

OR

You can append all array data with comma separator in one string object & store in shared preferences and when you get data then split string by comma.

Updated

    String[] array1 = new String[10];
    StringBuilder array1Data = new StringBuilder();

    for (int i = 0; i <= count; i++) {

        array1Data.append(array1[i]);
        array1Data.append(",");

    }

    setSharedPreferences("KEY", array1Data.toString()); // Custom method
}



回答2:


You can use JSONArray array for that. For insert value call array.put(String) or array.put(long). Insert into preferences as String by calling array.toString(). Get from preferences - array = new JSONArray(stringFromPreferences). Get value from array - array.getString(position) and array.getLong(postion).

You can also work with array.put(Object) and (String) array.get(position).



来源:https://stackoverflow.com/questions/29573941/how-to-store-array-values-of-type-string-and-biginteger-into-shared-preferen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!