Android sharedpreferences in arraylist

I created a simple game. In the end, the username and score should be on the highscore list. For this, I would like to save this data in shared preferences. I saw the message, and I'm trying to apply it to my application, but it closes. I don’t even know if I am doing it right. Therefore, I put these keywords (player, rating) in the arraylist. From there, I can get the values ​​in the list. 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();

It would also be good for me if I saved this data as follows:

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

and put it in ArrayList<String> LIST = new ArrayList<String>();

but I don’t know how to do it.

Could you guys help me with this?

: , . , , . , sg sharedpreferences, .

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);
+3
1

SharedPreferences Editor , . HashSet - . , ArrayList, , .

, Set , "Thomas" + "-" + "5".

: -

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

,

EDIT: API get/setStringSet(), :

1) . , [Tom, 1 "," Ed, 5 "], String, " Tom, 1 | Ed, 5 ". setString(..).

2) , getString(..), String.split("|"), . , , .

+3

All Articles