问题
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