I need to find a way of saving a HashMap to string to save in my SQLite database to retrieve on a later stage.
Here is my HashMap:
private Map<String, Bitmap> myMarkersHash new HashMap<String, Bitmap>();
Then when saving to the SQlite database:
contentValues.put(LocationsDB.FIELD_HASH, myMarkersHash);
But the problem is i get an error under put at contentValues.put with this error:
The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Map<String,Bitmap>)
So my question is how can i convert that to a string so that i can save it into my sqlite database?
Thanks
You can store your HashMap in your Database by converting it into String using Gson library.
This library will convert your object to String and you will get back that object whenever you want it.
Link to Download Lib : https://code.google.com/p/google-gson/
Convert Object into String :
Gson objGson= new Gson();
String strObject = objGson.toJson(myMarkersHash);
Bundle bundle = new Bundle();
bundle.putString("key", strObject);
Convert String into Object :
Gson gson = new Gson();
Type type = new TypeToken<Model>() {}.getType();
myMarkersHash = gson.fromJson(bundle.getString("key"), type);
Check this example as well : http://www.java2blog.com/2013/11/gson-example-read-and-write-json.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With