我已将Set置于共享偏好中:
Set<String> bodyClick = new HashSet<String>();
String panelMarked = new Boolean(panel.marked).toString();
String[] click = {panel.name,panelMarked,clickTimes};
String clickBody = Arrays.toString(click);
bodyClick.add(clickBody);
sharedEditor.putStringSet("click", bodyClick);
这就是我得到它的方式:
Set<String> click = preferences.getStringSet("click", new HashSet<String>());
if(click != null){
Iterator<String> iterator = click.iterator();
while(iterator.hasNext()){
String id = iterator.next();
}
}
例如,这是在字符串中:[左后挡泥板,假,0]
到目前为止,一切都还可以。
但是现在我想要为自己获得每个元素。如何在字符串中获得“Left rear fener”,在字符串中输入“false”,在字符串中输入“0”?
任何人都可以发布一些代码吗?
答案 0 :(得分:1)
我明白了:)
我的解决方案:
Set<String> click = preferences.getStringSet("click", new HashSet<String>());
if(click != null){
Iterator<String> iterator = click.iterator();
while(iterator.hasNext()){
String id = iterator.next();
int start = id.indexOf("[") + 1;
int end = id.indexOf("]")-1;
String items = String.copyValueOf(id.toCharArray(), start, end);
String[] ary = items.split(",");
}
}
答案 1 :(得分:0)
为什么不使用String [Split?] [1]
String id = iterator.next();
id = id.substring(1, id.length()-1)
String[] parts = id.split(",");
String part1 = parts[0]; // Left Near fender
String part2 = parts[1]; // false