我有一个字符串的通用映射(键,值),这个字段是Bean的一部分,我需要使其可被分解。
因此,我可以使用Parcel#writeMap方法。API文档说:
请改用writeBundle(Bundle)。将Map展开为当前dataPosition()处的包裹,如果需要则增加dataCapacity()。 Map键必须是String对象。 Map值使用writeValue(Object)写入,必须遵循该规范。 强烈建议使用writeBundle(Bundle)而不是此方法,因为Bundle类提供了类型安全的API,允许您在编组点避免神秘的类型错误。
那么,我可以迭代每个Map条目,并将其放入Bundle中,但我仍在寻找更智能的方法来实现。 Android SDK中是否有我错过的任何方法?
目前,我像这样做:
final Bundle bundle = new Bundle();
final Iterator<Entry<String, String>> iter = links.entrySet().iterator();
while(iter.hasNext())
{
final Entry<String, String> entry =iter.next();
bundle.putString(entry.getKey(), entry.getValue());
}
parcel.writeBundle(bundle);