Storing a 1D array of strings in Shared Preferences










0















I have two arrays, the first consists of 9 buttons. The second can hold 9 Strings. I have a method called getPlayerChoiceText to populate the String array with the text that is set on each button from the playerchoice Array. How can I save this text using SharedPreferences?



private String getPlayerChoiceText()

playerchoiceText[0] = playerchoice[0].getText().toString();
playerchoiceText[1] = playerchoice[1].getText().toString();
playerchoiceText[2] = playerchoice[2].getText().toString();
playerchoiceText[3] = playerchoice[3].getText().toString();
playerchoiceText[4] = playerchoice[4].getText().toString();
playerchoiceText[5] = playerchoice[5].getText().toString();
playerchoiceText[6] = playerchoice[6].getText().toString();
playerchoiceText[7] = playerchoice[7].getText().toString();
playerchoiceText[8] = playerchoice[8].getText().toString();

return playerchoiceText;


private void saveData()

SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
playerchoiceText = getPlayerChoiceText();










share|improve this question



















  • 1





    Why do you maintain two arrays for this? Can't you use a Map<Button, String>? I think SharedPreferences are storing key-value pairs, which would go very well with a Map.

    – deHaar
    Nov 15 '18 at 15:19












  • If it has to be SharedPreferences, concatenate your array of string into one long String and use delimiters between them (for example :).

    – Endre Börcsök
    Nov 15 '18 at 15:22
















0















I have two arrays, the first consists of 9 buttons. The second can hold 9 Strings. I have a method called getPlayerChoiceText to populate the String array with the text that is set on each button from the playerchoice Array. How can I save this text using SharedPreferences?



private String getPlayerChoiceText()

playerchoiceText[0] = playerchoice[0].getText().toString();
playerchoiceText[1] = playerchoice[1].getText().toString();
playerchoiceText[2] = playerchoice[2].getText().toString();
playerchoiceText[3] = playerchoice[3].getText().toString();
playerchoiceText[4] = playerchoice[4].getText().toString();
playerchoiceText[5] = playerchoice[5].getText().toString();
playerchoiceText[6] = playerchoice[6].getText().toString();
playerchoiceText[7] = playerchoice[7].getText().toString();
playerchoiceText[8] = playerchoice[8].getText().toString();

return playerchoiceText;


private void saveData()

SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
playerchoiceText = getPlayerChoiceText();










share|improve this question



















  • 1





    Why do you maintain two arrays for this? Can't you use a Map<Button, String>? I think SharedPreferences are storing key-value pairs, which would go very well with a Map.

    – deHaar
    Nov 15 '18 at 15:19












  • If it has to be SharedPreferences, concatenate your array of string into one long String and use delimiters between them (for example :).

    – Endre Börcsök
    Nov 15 '18 at 15:22














0












0








0


1






I have two arrays, the first consists of 9 buttons. The second can hold 9 Strings. I have a method called getPlayerChoiceText to populate the String array with the text that is set on each button from the playerchoice Array. How can I save this text using SharedPreferences?



private String getPlayerChoiceText()

playerchoiceText[0] = playerchoice[0].getText().toString();
playerchoiceText[1] = playerchoice[1].getText().toString();
playerchoiceText[2] = playerchoice[2].getText().toString();
playerchoiceText[3] = playerchoice[3].getText().toString();
playerchoiceText[4] = playerchoice[4].getText().toString();
playerchoiceText[5] = playerchoice[5].getText().toString();
playerchoiceText[6] = playerchoice[6].getText().toString();
playerchoiceText[7] = playerchoice[7].getText().toString();
playerchoiceText[8] = playerchoice[8].getText().toString();

return playerchoiceText;


private void saveData()

SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
playerchoiceText = getPlayerChoiceText();










share|improve this question
















I have two arrays, the first consists of 9 buttons. The second can hold 9 Strings. I have a method called getPlayerChoiceText to populate the String array with the text that is set on each button from the playerchoice Array. How can I save this text using SharedPreferences?



private String getPlayerChoiceText()

playerchoiceText[0] = playerchoice[0].getText().toString();
playerchoiceText[1] = playerchoice[1].getText().toString();
playerchoiceText[2] = playerchoice[2].getText().toString();
playerchoiceText[3] = playerchoice[3].getText().toString();
playerchoiceText[4] = playerchoice[4].getText().toString();
playerchoiceText[5] = playerchoice[5].getText().toString();
playerchoiceText[6] = playerchoice[6].getText().toString();
playerchoiceText[7] = playerchoice[7].getText().toString();
playerchoiceText[8] = playerchoice[8].getText().toString();

return playerchoiceText;


private void saveData()

SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
playerchoiceText = getPlayerChoiceText();







java android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:51









Fantômas

32.8k156490




32.8k156490










asked Nov 15 '18 at 15:14









user10431501user10431501

448




448







  • 1





    Why do you maintain two arrays for this? Can't you use a Map<Button, String>? I think SharedPreferences are storing key-value pairs, which would go very well with a Map.

    – deHaar
    Nov 15 '18 at 15:19












  • If it has to be SharedPreferences, concatenate your array of string into one long String and use delimiters between them (for example :).

    – Endre Börcsök
    Nov 15 '18 at 15:22













  • 1





    Why do you maintain two arrays for this? Can't you use a Map<Button, String>? I think SharedPreferences are storing key-value pairs, which would go very well with a Map.

    – deHaar
    Nov 15 '18 at 15:19












  • If it has to be SharedPreferences, concatenate your array of string into one long String and use delimiters between them (for example :).

    – Endre Börcsök
    Nov 15 '18 at 15:22








1




1





Why do you maintain two arrays for this? Can't you use a Map<Button, String>? I think SharedPreferences are storing key-value pairs, which would go very well with a Map.

– deHaar
Nov 15 '18 at 15:19






Why do you maintain two arrays for this? Can't you use a Map<Button, String>? I think SharedPreferences are storing key-value pairs, which would go very well with a Map.

– deHaar
Nov 15 '18 at 15:19














If it has to be SharedPreferences, concatenate your array of string into one long String and use delimiters between them (for example :).

– Endre Börcsök
Nov 15 '18 at 15:22






If it has to be SharedPreferences, concatenate your array of string into one long String and use delimiters between them (for example :).

– Endre Börcsök
Nov 15 '18 at 15:22













3 Answers
3






active

oldest

votes


















2














I have got the same problem. I solved it by using JSONArray.



JSONArray choices = new JSONArray();
choices.put("1");
choices.put("2");
choices.put("3");

// Save
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("choices", choices.toString());

// Retrieve
choices = new JSONArray(sharedPreferences.getString("choices", ""));


In this way, you can easily do Insert and Delete operations. I hope this helps.






share|improve this answer


















  • 1





    Good suggestion +1. I didn't think of that.

    – Tim Biegeleisen
    Nov 16 '18 at 1:44


















0














Android's SharedPreferences operates as a key value store, and does not allow you to directly store Java objects. So, representing your player choice text values as a map would make more sense, if you want to store them using shared preferences.



There is one trick you might be able to use here, if you wanted to continue representing your choice texts as an array. You could store the texts using a delimiter, e.g. pipe:



String choices = String.join("|", playerchoiceText);
SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
prefs.edit().putString("choices", choices).apply();


And then, on the way out:



SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
String playerchoiceText = prefs.getString("choices", "").split("\|");





share|improve this answer























  • Would this be done better if i used a List? Instead of a regular 1d array?

    – user10431501
    Nov 15 '18 at 15:48











  • The issue still remains the same, namely that shared prefs using a map functionality to store its data. So, you would need to adapt to a map interface.

    – Tim Biegeleisen
    Nov 15 '18 at 16:09











  • Ok, I will try it out, thanks for all yer help.

    – user10431501
    Nov 15 '18 at 16:10


















0














Use put/getStringSet():



private void saveData()

SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
String playerchoiceText = getPlayerChoiceText();
editor.putStringSet("player_choice", new HashSet<T>(Arrays.asList(playerchoiceText));
editor.commit();






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322490%2fstoring-a-1d-array-of-strings-in-shared-preferences%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    I have got the same problem. I solved it by using JSONArray.



    JSONArray choices = new JSONArray();
    choices.put("1");
    choices.put("2");
    choices.put("3");

    // Save
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("choices", choices.toString());

    // Retrieve
    choices = new JSONArray(sharedPreferences.getString("choices", ""));


    In this way, you can easily do Insert and Delete operations. I hope this helps.






    share|improve this answer


















    • 1





      Good suggestion +1. I didn't think of that.

      – Tim Biegeleisen
      Nov 16 '18 at 1:44















    2














    I have got the same problem. I solved it by using JSONArray.



    JSONArray choices = new JSONArray();
    choices.put("1");
    choices.put("2");
    choices.put("3");

    // Save
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("choices", choices.toString());

    // Retrieve
    choices = new JSONArray(sharedPreferences.getString("choices", ""));


    In this way, you can easily do Insert and Delete operations. I hope this helps.






    share|improve this answer


















    • 1





      Good suggestion +1. I didn't think of that.

      – Tim Biegeleisen
      Nov 16 '18 at 1:44













    2












    2








    2







    I have got the same problem. I solved it by using JSONArray.



    JSONArray choices = new JSONArray();
    choices.put("1");
    choices.put("2");
    choices.put("3");

    // Save
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("choices", choices.toString());

    // Retrieve
    choices = new JSONArray(sharedPreferences.getString("choices", ""));


    In this way, you can easily do Insert and Delete operations. I hope this helps.






    share|improve this answer













    I have got the same problem. I solved it by using JSONArray.



    JSONArray choices = new JSONArray();
    choices.put("1");
    choices.put("2");
    choices.put("3");

    // Save
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("choices", choices.toString());

    // Retrieve
    choices = new JSONArray(sharedPreferences.getString("choices", ""));


    In this way, you can easily do Insert and Delete operations. I hope this helps.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 18:09









    Uma SankarUma Sankar

    182316




    182316







    • 1





      Good suggestion +1. I didn't think of that.

      – Tim Biegeleisen
      Nov 16 '18 at 1:44












    • 1





      Good suggestion +1. I didn't think of that.

      – Tim Biegeleisen
      Nov 16 '18 at 1:44







    1




    1





    Good suggestion +1. I didn't think of that.

    – Tim Biegeleisen
    Nov 16 '18 at 1:44





    Good suggestion +1. I didn't think of that.

    – Tim Biegeleisen
    Nov 16 '18 at 1:44













    0














    Android's SharedPreferences operates as a key value store, and does not allow you to directly store Java objects. So, representing your player choice text values as a map would make more sense, if you want to store them using shared preferences.



    There is one trick you might be able to use here, if you wanted to continue representing your choice texts as an array. You could store the texts using a delimiter, e.g. pipe:



    String choices = String.join("|", playerchoiceText);
    SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
    prefs.edit().putString("choices", choices).apply();


    And then, on the way out:



    SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
    String playerchoiceText = prefs.getString("choices", "").split("\|");





    share|improve this answer























    • Would this be done better if i used a List? Instead of a regular 1d array?

      – user10431501
      Nov 15 '18 at 15:48











    • The issue still remains the same, namely that shared prefs using a map functionality to store its data. So, you would need to adapt to a map interface.

      – Tim Biegeleisen
      Nov 15 '18 at 16:09











    • Ok, I will try it out, thanks for all yer help.

      – user10431501
      Nov 15 '18 at 16:10















    0














    Android's SharedPreferences operates as a key value store, and does not allow you to directly store Java objects. So, representing your player choice text values as a map would make more sense, if you want to store them using shared preferences.



    There is one trick you might be able to use here, if you wanted to continue representing your choice texts as an array. You could store the texts using a delimiter, e.g. pipe:



    String choices = String.join("|", playerchoiceText);
    SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
    prefs.edit().putString("choices", choices).apply();


    And then, on the way out:



    SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
    String playerchoiceText = prefs.getString("choices", "").split("\|");





    share|improve this answer























    • Would this be done better if i used a List? Instead of a regular 1d array?

      – user10431501
      Nov 15 '18 at 15:48











    • The issue still remains the same, namely that shared prefs using a map functionality to store its data. So, you would need to adapt to a map interface.

      – Tim Biegeleisen
      Nov 15 '18 at 16:09











    • Ok, I will try it out, thanks for all yer help.

      – user10431501
      Nov 15 '18 at 16:10













    0












    0








    0







    Android's SharedPreferences operates as a key value store, and does not allow you to directly store Java objects. So, representing your player choice text values as a map would make more sense, if you want to store them using shared preferences.



    There is one trick you might be able to use here, if you wanted to continue representing your choice texts as an array. You could store the texts using a delimiter, e.g. pipe:



    String choices = String.join("|", playerchoiceText);
    SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
    prefs.edit().putString("choices", choices).apply();


    And then, on the way out:



    SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
    String playerchoiceText = prefs.getString("choices", "").split("\|");





    share|improve this answer













    Android's SharedPreferences operates as a key value store, and does not allow you to directly store Java objects. So, representing your player choice text values as a map would make more sense, if you want to store them using shared preferences.



    There is one trick you might be able to use here, if you wanted to continue representing your choice texts as an array. You could store the texts using a delimiter, e.g. pipe:



    String choices = String.join("|", playerchoiceText);
    SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
    prefs.edit().putString("choices", choices).apply();


    And then, on the way out:



    SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_KEY, Context.MODE_PRIVATE);
    String playerchoiceText = prefs.getString("choices", "").split("\|");






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 15:25









    Tim BiegeleisenTim Biegeleisen

    233k1399157




    233k1399157












    • Would this be done better if i used a List? Instead of a regular 1d array?

      – user10431501
      Nov 15 '18 at 15:48











    • The issue still remains the same, namely that shared prefs using a map functionality to store its data. So, you would need to adapt to a map interface.

      – Tim Biegeleisen
      Nov 15 '18 at 16:09











    • Ok, I will try it out, thanks for all yer help.

      – user10431501
      Nov 15 '18 at 16:10

















    • Would this be done better if i used a List? Instead of a regular 1d array?

      – user10431501
      Nov 15 '18 at 15:48











    • The issue still remains the same, namely that shared prefs using a map functionality to store its data. So, you would need to adapt to a map interface.

      – Tim Biegeleisen
      Nov 15 '18 at 16:09











    • Ok, I will try it out, thanks for all yer help.

      – user10431501
      Nov 15 '18 at 16:10
















    Would this be done better if i used a List? Instead of a regular 1d array?

    – user10431501
    Nov 15 '18 at 15:48





    Would this be done better if i used a List? Instead of a regular 1d array?

    – user10431501
    Nov 15 '18 at 15:48













    The issue still remains the same, namely that shared prefs using a map functionality to store its data. So, you would need to adapt to a map interface.

    – Tim Biegeleisen
    Nov 15 '18 at 16:09





    The issue still remains the same, namely that shared prefs using a map functionality to store its data. So, you would need to adapt to a map interface.

    – Tim Biegeleisen
    Nov 15 '18 at 16:09













    Ok, I will try it out, thanks for all yer help.

    – user10431501
    Nov 15 '18 at 16:10





    Ok, I will try it out, thanks for all yer help.

    – user10431501
    Nov 15 '18 at 16:10











    0














    Use put/getStringSet():



    private void saveData()

    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    String playerchoiceText = getPlayerChoiceText();
    editor.putStringSet("player_choice", new HashSet<T>(Arrays.asList(playerchoiceText));
    editor.commit();






    share|improve this answer



























      0














      Use put/getStringSet():



      private void saveData()

      SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
      SharedPreferences.Editor editor = sharedPreferences.edit();
      String playerchoiceText = getPlayerChoiceText();
      editor.putStringSet("player_choice", new HashSet<T>(Arrays.asList(playerchoiceText));
      editor.commit();






      share|improve this answer

























        0












        0








        0







        Use put/getStringSet():



        private void saveData()

        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String playerchoiceText = getPlayerChoiceText();
        editor.putStringSet("player_choice", new HashSet<T>(Arrays.asList(playerchoiceText));
        editor.commit();






        share|improve this answer













        Use put/getStringSet():



        private void saveData()

        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String playerchoiceText = getPlayerChoiceText();
        editor.putStringSet("player_choice", new HashSet<T>(Arrays.asList(playerchoiceText));
        editor.commit();







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 17:00









        barmaleybarmaley

        12.2k1563126




        12.2k1563126



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322490%2fstoring-a-1d-array-of-strings-in-shared-preferences%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto