发送LinkedHashMap到意图 [英] Sending LinkedHashMap to intent

查看:129
本文介绍了发送LinkedHashMap到意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将LinkedHashMap发送到另一个Intent.但是我不知道可以使用哪种额外方法.

I want send LinkedHashMap to another Intent. But I don't known what method for extras is allowable.

Bundle extras = getIntent().getExtras();
  LinkedHashMap<Integer, String[]> listItems = extras.get(LIST_TXT);

推荐答案

您不能可靠地发送LinkedHashMap作为额外的Intent.当您使用LinkedHashMap调用putExtra()时,Android会发现该对象实现了Map接口,因此它将名称/值对序列化为Intent中的附加项Bundle.当您想在另一侧提取它时,得到的是HashMap,而不是LinkedHashMap.不幸的是,您得到的HashMap失去了顺序,这是您首先要使用LinkedHashMap的原因.

You cannot reliably send a LinkedHashMap as an Intent extra. When you call putExtra() with a LinkedHashMap, Android sees that the object implements the Map interface, so it serializes the name/value pairs into the extras Bundle in the Intent. When you want to extract it on the other side, what you get is a HashMap, not a LinkedHashMap. Unfortunately, this HashMap that you get has lost the ordering that was the reason you wanted to use a LinkedHashMap in the first place.

执行此操作的唯一可靠方法是将LinkedHashMap转换为有序数组,将其放入Intent,在接收端从Intent提取数组,然后重新创建.

The only reliable way to do this is to convert the LinkedHashMap to an ordered array, put the array into the Intent, extract the array from the Intent on the receiving end, and then recreate the LinkedHashMap.

请参阅

See my answer to this question for more gory details.

这篇关于发送LinkedHashMap到意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆