如何将Hashmap转换为String以保存在SQLite数据库中?

如何将Hashmap转换为String以保存在SQLite数据库中?,第1张

概述我需要找到一种方法来将HashMap保存为字符串以保存在我的SQLite数据库中以便在以后的阶段进行检索.这是我的HashMap:privateMap<String,Bitmap>myMarkersHashnewHashMap<String,Bitmap>();然后保存到SQlite数据库时:contentValues.put(LocationsDB.FIELD_HASH,myMarker

我需要找到一种方法来将HashMap保存为字符串以保存在我的sqlite数据库中以便在以后的阶段进行检索.

这是我的HashMap:

private Map<String, Bitmap> myMarkersHash new HashMap<String, Bitmap>();

然后保存到sqlite数据库时:

contentValues.put(LocationsDB.FIELD_HASH, myMarkersHash);

但问题是我在contentValues.put下输入错误时出现此错误:

The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Map<String,Bitmap>)

所以我的问题是如何将其转换为字符串,以便我可以将其保存到我的sqlite数据库?

谢谢

解决方法:

您可以使用Gson库将HashMap转换为String,从而将HashMap存储在数据库中.
该库将您的对象转换为String,您可以随时获取该对象.
链接到下载lib:https://code.google.com/p/google-gson/

将对象转换为字符串:

Gson objGson= new Gson();String strObject = objGson.toJson(myMarkersHash);Bundle bundle = new Bundle();bundle.putString("key", strObject);

将String转换为Object:

Gson gson = new Gson();Type type = new Typetoken<Model>() {}.getType();myMarkersHash = gson.fromJson(bundle.getString("key"), type);

也请查看此示例:http://www.java2blog.com/2013/11/gson-example-read-and-write-json.html

总结

以上是内存溢出为你收集整理的如何将Hashmap转换为String以保存在SQLite数据库中?全部内容,希望文章能够帮你解决如何将Hashmap转换为String以保存在SQLite数据库中?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1107720.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存