共享preferences思想促进数组 [英] sharedpreferences puting an array
问题描述
我如何使用这个code,以添加阵列,后来找回?
我可以用一个简单的循环?
共享preferences设置= getShared preferences(isChk,0);
共享preferences.Editor编辑= settings.edit();
editor.putBoolean(钥匙,ArrayValue [I]);
editor.commit();
和我怎样才能然后检索他们每个元素存储在一个数组变量我可以用这个?
的for(int i = 0; I< myArr.size();我++){
键[I] = settings.getBoolean(isChk,FALSE);
isChkb.add(键[I]);
}
要存储在共享preferences $ C的
阵列
$ C>,你可以把你的数组值
在字符串
,然后的商店的这字符串
..如果你想你的数组值
(在本例中,存储在一个字符串),您的 GET 的的字符串
和解析
使用它的StringTokenizer
是这样的:(在我的例子,我将存储从其获取布尔数组)
-
把你的布尔值转换成字符串,一个字符分隔每个INT,例如一个逗号,然后将它们保存为一个字符串:
共享preferences preFS = GET preferences(MODE_PRIVATE);
布尔[] =名单布尔新[10];
StringBuilder的海峡=新的StringBuilder();
的for(int i = 0; I< List.length的数字,我++){
str.append(名单[I])追加();
}
。prefs.edit()putBoolean(钥匙,str.toString())提交(); -
获取字符串和使用的StringTokenizer解析:
字符串savedString = prefs.getString(字符串,);
StringTokenizer的ST =新的StringTokenizer(savedString,,);
布尔[] = savedList新布尔[10];
对(INT I = 0; I&小于10;我++){
savedList [I] =通过Boolean.valueOf(st.nextToken();
}
how can i use this code in order to add an array and retrieve it later? can i use a simple for loop ?
SharedPreferences settings = getSharedPreferences("isChk", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("keys",ArrayValue[i] );
editor.commit();
and how can i then retrieve them and store each element in an array variable can i use this ?
for( int i=0; i<myArr.size(); i++){
keys[i] = settings.getBoolean("isChk", false);
isChkb.add(keys[i]);
}
To store an array
in sharedPreferences
, you can put your array values
in a String
, then store this String
.. and if you you want to get your array value
(in our case ,stored in a string) , you get the string
and parse
it using StringTokenizer
like this : (In my example I will store and retreive an array of Boolean)
Put your booleans into a string, delimiting every int by a character, for example a comma, and then save them as a string:
SharedPreferences prefs = getPreferences(MODE_PRIVATE); Boolean[] list = new Boolean[10]; StringBuilder str = new StringBuilder(); for (int i = 0; i < list.length; i++) { str.append(list[i]).append(","); } prefs.edit().putBoolean("keys", str.toString()).commit();
Get the string and parse it using StringTokenizer:
String savedString = prefs.getString("string", ""); StringTokenizer st = new StringTokenizer(savedString, ","); Boolean[] savedList = new Boolean[10]; for (int i = 0; i < 10; i++) { savedList[i] = Boolean.valueOf(st.nextToken(); }
这篇关于共享preferences思想促进数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!