0

My App: has two activities, one to create two strings (name and message), which are saved into a hashmap, transferred into JSON and saved in a SharedPreference. The other activity fetches this SharedPreference, transfers it into a hashmap again. Then the map is "read out" by an Iterator.

Supposed to be: My Idea is, that if I do this process several times, the iterator "reads out" all the entries to the hashmap.

Problem: However, only the last input gets displayed.

Question: Why is only the last input displayed? Am I overriding the hashmap or parts of it? And if so, how can I avoid it? Or what else am I doing wrong?


Activity 1:

LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
    map.put(message, name);

    Gson gson = new Gson();
    String hashMapString = gson.toJson(map);

    SharedPreferences prefs = getSharedPreferences("SP", MODE_PRIVATE);
    prefs.edit().putString("hashString", hashMapString).apply();

Activity 2:

Gson gson = new Gson();
        SharedPreferences prefs = getSharedPreferences("SP", MODE_PRIVATE);

        String storedHashMapString = prefs.getString("hashString", "Error");
        java.lang.reflect.Type type = new TypeToken<LinkedHashMap<String, String>>(){}.getType();
        LinkedHashMap<String, String> map = gson.fromJson(storedHashMapString, type);


    Iterator myIterator = map.keySet().iterator();
    while(myIterator.hasNext()) {
        String key=(String)myIterator.next();
        String value=(String)map.get(key);
        Toast.makeText(getApplicationContext(), "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
    }
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
Makarele
  • 59
  • 9
  • Please provide examples of name/messages couples. More specifically, do you use multiple times the same "message" (which is your key in the first map here) – minioim Aug 08 '17 at 17:23
  • The keys of the map need to be unique or else the new entry overrides the original entry. – isaace Aug 08 '17 at 17:25
  • It looks okay from what I can tell. Have you tried logging hashMapString to LogCat? – Da-Jin Aug 08 '17 at 17:25
  • Just for example - When I insert (cat|one) it gives me (cat|one). When I insert (cow|two) afterwards it only gives my (cow|two), but I want to retrieve both. – Makarele Aug 08 '17 at 17:29
  • you need something `prefs.edit().putString("hashString", prefs.getString("hashString", "")+hashMapString).apply();` – Pavneet_Singh Aug 08 '17 at 17:33

2 Answers2

2

while doing this

prefs.edit().putString("hashString", hashMapString).apply();

you are just saving values of newly created hashmap but old losing values which were saved previously using hashString key

Solution: fetch the old value and save it along with new values

Option one : fetch previous string , convert it to jsonobject then add the values to map

Reference : creating Hashmap from a JSON String

Option two : if you have only string key-value pair then

  • remove { and } from both new and old map strings then simply combine them and add { and } boundary symbols

     String allValues ="{"+((prefs.getString("hashString", "")+hashMapString)
                                      .replace("{","").replace("}",""))+"}";
    

then later save it

prefs.edit().putString("hashString", allValues).apply();
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

you are seeing the last input because putString overwrites whatever was there in the first place

https://developer.android.com/reference/android/content/SharedPreferences.Editor.html

Nick
  • 585
  • 4
  • 11