简体   繁体   English

如何在Android中的SharedPreferences中存储整数数组?

[英]How can i store an Integer Array in SharedPreferences in Android?

I have an Array called Upval that has 16 Integer values that I would like to store in my SharedPreferences without creating individual ones for each, but SharedPrefernces won't allow Array's, what is the simplest way of doing this? 我有一个名为Upval的数组,它有16个整数值,我想存储在我的SharedPreferences中而不为每个创建单独的值,但SharedPrefernces不允许使用Array,这是最简单的方法吗? The declaration looks something like this: 声明看起来像这样:

Integer[] UpVal = new Integer[16];

You can store it as a String by transforming it: 您可以通过转换将其存储为String:

Arrays.toString(upVal)

To get it back and convert a String to an Integer array is trivial. 要取回它并将String转换为Integer数组是微不足道的。

You can serialize an array to String using TextUtils.join(";", myInts) and the deserialize it back using something like TextUtils. 您可以使用TextUtils.join(“;”,myInts)将数组序列化为String,并使用TextUtils之类的方法将其反序列化。 SimpleStringSplitter or implement your own TextUtils.StringSplitter. SimpleStringSplitter或实现您自己的TextUtils.StringSplitter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM