通过Bundle传递Map类型数据

来源:互联网 发布:数据爆炸是什么 编辑:程序博客网 时间:2024/05/27 00:55

可能通过其他更好的方法可以传递,先记录我知道的这个方法吧。大家指正和帮忙改进。


大家知道Bundle对象只能传递一些基本类型和字符串之类的,不能直接传入Map类型的数据,所以就暂时在情急之下想了一个复杂的方法,虽然不好,但有效。

但是只能传递参数都为基本类型或者String类型的。


Map<String, String[]> map;Bundle bundle = new Bundle();Set<String> keySet = map.keySet();Iterator<String> iter = keySet.iterator();                    while(iter.hasNext()){    String key = iter.next();    bundle.putStringArray(key, map.get(key));}intent.putExtra("map", bundle);


获取的方法如下:

Map<String, String[]> map;Bundle bundle = intent.getBundleExtra("map");Set<String> keySet = bundle.keySet();   // 得到bundle中所有的keyIterator<String> iter = keySet.iterator();while(iter.hasNext()){    String key = iter.next();    map.put(key, bundle.getStringArray(key));}




暂时写了一个方法,知道可以实现一些序列号接口或者Parcelable接口,但是还没有时间来实验,等以后继续更新