0

I try to get my saved records from sharedPreferences, but i cant. Always nothing show.

In LostDialogFragment i save the array, i debugging this and this always save data. I get References from MainGameFragment

 public void saveArray() {
    SharedPreferences prefs = getContext().getSharedPreferences("user_and_score", Context.MODE_PRIVATE);
    SharedPreferences.Editor edit = prefs.edit();
    Set<String> set = new HashSet<>();
    set.addAll(MainGameFragment.LEADERBOARDUSER);
    edit.putStringSet("user_and_score", set);
    edit.apply();
}

Here is reference in MainGameFragment

 public static ArrayList<String> LEADERBOARDUSER = new ArrayList<>();

And in SettingsFragment i try get saved Array

 private ArrayList<String> leaderBoardList = new ArrayList<>();


case LEADER_BOARD:
            recyclerView.setVisibility(View.VISIBLE);
            SharedPreferences prefs = getContext().getSharedPreferences("user_and_score", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = prefs.edit();
            Set<String> set = new HashSet<>();
            edit.putStringSet("user_and_score", set);
            set.addAll(leaderBoardList);
            edit.apply();
            adapter = new CustomAdapterForSettingsFragment(leaderBoardList);
            recyclerView.setAdapter(adapter);
            break;

My question is how to get saved array ?

Or i didnt save. I debugging now and no one is saved, Help me with save array in Shared too

Rodriquez
  • 981
  • 1
  • 7
  • 21
  • You need to use the method [getStringSet](https://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet(java.lang.String, java.util.Set)) to get your saved set from shared preferences. – Prerak Sola Feb 27 '17 at 09:07

5 Answers5

1

For your saving the Set problem,Try the followings:- 1.) Give some different key to save your set, like

 edit.putStringSet("your_set", set);

2.) Use

 edit.commit()

And to get your Set, try the following :-

SharedPreferences prefs = getContext().getSharedPreferences("user_and_score", Context.MODE_PRIVATE);

Set<String> newSet = prefs.getStringSet("your_set", new HashSet<String>());

By doing this, you can get your saved set in newSet. Thanks!!!

Nazim ch
  • 834
  • 8
  • 20
0

You can find the answer here Save ArrayList to SharedPreferences. Also I think you have a problem in

edit.apply();

try

edit.commit();
Community
  • 1
  • 1
Davit Avetisyan
  • 129
  • 1
  • 9
  • While the link may answer the OP's question, it is not recommended to write link-only answers. In the future if the link gets modified, the answer will not be useful to future visitors. Instead post the relevant snippets that can answer the question in your answer and add the link as a reference. – Prerak Sola Feb 27 '17 at 09:10
0

Try this,

dependencies {
    compile 'com.google.code.gson:gson:2.2.+'
}       

Set the values

List<String> leaderBoardList   = new ArrayList<>();

Gson gson = new Gson();
String jsonText = gson.toJson(leaderBoardList);
prefsEditor.putString("key", jsonText);
prefsEditor.commit();

Retrieve the values

List<String> leaderBoardList   = new ArrayList<>();
Gson gson = new Gson();
String jsonText = Prefs.getString("key", null);
String[] text = gson.fromJson(jsonText, String[].class);  //EDIT: gso to gson

leaderBoardList  = Arrays.asList(text);
leaderBoardList  = new ArrayList(leaderBoardList );
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • when i try retrieve i have bug: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 3 – Rodriquez Feb 27 '17 at 09:41
0

I had a similar problem with shared preferences, I could not get a previously saved String.

A solution that worked for me was to get the SharedPrefs instance via the PreferenceManager:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());

And then get the String Set:

Set<String> set = sharedPref.getStringSet("your_set", new HashSet<String>());
fijili
  • 39
  • 4
0

The best way is:

1) convert your list in to JsonArray say jsonArrayPref

2) save jsonArrayPref.toString() to shared preferences

3) when ever you want that List then get the jsonArrayPref from the preferences in the form of string

4) and then convert it in to JsonArray and render you data and use it