Android - Setting and Fetching a StringSet from SharedPreferences?

后端 未结 2 977
执笔经年
执笔经年 2021-01-17 20:03

Good day, I am building an Android Application. I want to store a set of strings in the Preferences to order to track who used the application based on their log in informat

相关标签:
2条回答
  • 2021-01-17 20:16

    Create an editor Object first :

    SharedPreferences.Editor editor = prefs.edit();
    

    And use the editor object to store and fetch your string set like this :

    editor.putStringSet("List", set);
    editor.apply();
    
    Set<String> fetch = editor.getStringSet("List", null);
    
    0 讨论(0)
  • 2021-01-17 20:16

    You could write your results to a JSON String and store them in Shared Preferences like this: https://stackoverflow.com/a/5968562/617044

    But if you choose to go down the route you're currently on then you're going to have to store a parcelable.

    As noted in another answer; you can use putStringSet(), getStringSet() if you're building for API 11+.

    0 讨论(0)
提交回复
热议问题