在我的Android应用程序中,我尝试使用内部存储来存储地图结构,如:Map<String, Map<String, String>>
。我研究过使用SharedPreferences
,但正如你所知,这只适用于存储原始数据类型。我试图使用FileOutputStream
,但它只能让我以字节写...我需要以某种方式序列化Hashmap,然后写入文件?如何在Android内部存储数据结构(如HashMap)?
我试过通过http://developer.android.com/guide/topics/data/data-storage.html#filesInternal阅读,但我似乎无法找到我的解决方案。
这里是什么,我试图做一个例子:
private void storeEventParametersInternal(Context context, String eventId, Map<String, String> eventDetails){
Map<String,Map<String,String>> eventStorage = new HashMap<String,Map<String,String>>();
Map<String, String> eventData = new HashMap<String, String>();
String REQUEST_ID_KEY = randomString(16);
. //eventData.put...
. //eventData.put...
eventStorage.put(REQUEST_ID_KEY, eventData);
FileOutputStream fos = context.openFileOutput(EVENT_FILENAME, Context.MODE_PRIVATE);
fos.write(eventStorage) //This is wrong but I need to write to file for later access..
}
什么是内部在Android应用程序存储这种类型的数据结构的最佳方法?对不起,如果这看起来像一个愚蠢的问题,我是非常新的Android。提前致谢。