发送LinkedHashMap到意图 [英] Sending LinkedHashMap to intent
问题描述
我想将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屋!