简体   繁体   中英

How handle data from sharedpreferences getStringSet?

I had put the Set into sharedpreferences:

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);

and this is how I get it:

Set<String> click = preferences.getStringSet("click", new HashSet<String>());

if(click != null){

    Iterator<String> iterator = click.iterator();

    while(iterator.hasNext()){
        String id = iterator.next();
    }   
}

For example this is in the string: [Left rear fender, false, 0]

So far all is okay and working.

But now I want get each element for his own. How can I get "Left rear fener" in a String, "false" in a string and "0" in a string?

Can anybody post some code?

I got it :)

my solution:

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(",");
    }
}

Why not use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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