erdomester
erdomester

Reputation: 11829

Android sharedpreferences into arraylist

I created a simple game. At the end the user's name and score is supposed to get into a highscore list. For this i would like to store these data in sharedpreferences. I saw a post and i am trying to apply it to my app but it force closes. I don't even know if this is the right thing i am doing. So i put these keypairs (player, score) into an arraylist. From there i can get the values out into a listview. This is just an example.

SharedPreferences.Editor scoreEditor = myScores.edit();
        scoreEditor.putString("PLAYER", "Thomas");
        scoreEditor.putString("SCORE", "5");
        scoreEditor.commit();


        final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
        Map<String, ?> items = myScores.getAll();

        for(String s : items.keySet()){
            HashMap<String,String> hmap = new HashMap<String,String>();
            hmap.put("PLAYER", s);
            hmap.put("SCORE", items.get(s).toString());

            LIST.add(hmap);
        }

        Toast.makeText(Start.this, "LIST size: "+LIST.size(), Toast.LENGTH_LONG).show();

For me it would also be okay if i store these data like this:

scoreEditor.putString("DATA", "Thomas" + "-" + "5");

and put that into ArrayList<String> LIST = new ArrayList<String>();

but i don't know how to do it.

Could you guys help me with this?

Edit: So i could go another way as Haphazard suggested. I put together this code, but i don't know if this is the way to do it. I haven't tested it yet, as sg is wrong with the sharedpreferences and i am still trying to figure it out.

SharedPreferences.Editor scoreEditor = myScores.edit();
scoreEditor.putString("DATA", "Thomas" + "-" + "5");
scoreEditor.commit();


HashSet<String> hset=new HashSet<String>();
hset.addAll((Collection<? extends String>) myScores.getAll());

ArrayList<String> LIST = new ArrayList<String>(hset);

Upvotes: 3

Views: 7505

Answers (1)

Haphazard
Haphazard

Reputation: 10948

The SharedPreferences Editor does not accept Lists but it does accept Sets. You could convert your List into a HashSet or something similar and store it like that. When your read it back, convert it into an ArrayList, sort it if needed and you're good to go.

Please note that the Set has to be a set of Strings so you will have to stick with your "Thomas" + "-" + "5" setup.

Edit: To your new update, I was thinking more of something like

//Retrieve the values
Set<String> set = new HashSet<String>();
set = myScores.getStringSet("key", null);

//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();

That code is untested, but it should work

EDIT: If your API level is below the get/setStringSet() level then you can try this:

1) Turn your list of high scores into a delimited string. That means if you had ["Tom, 1", "Ed, 5"] you could loop through it and turn it into a String like "Tom, 1|Ed, 5". You can easily store that using setString(..).

2) When you want to read the values back, perform a getString(..) and then String.split("|") to get the original list back. Well, it returns an array but that can be converted to a list easily enough.

Upvotes: 3

Related Questions