捆绑中的Android HashMap?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11452859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android HashMap in Bundle?
提问by Marcus Toepper
The android.os.Message
uses a Bundle
to send with it's sendMessage-method. Therefore, is it possible to put a HashMap
inside a Bundle
?
在android.os.Message
使用了Bundle
与它的sendMessage法派。因此,是否可以将 a 放入 aHashMap
内Bundle
?
回答by ρяσ?ρ?я K
try as:
尝试如下:
Bundle extras = new Bundle();
extras.putSerializable("HashMap",hashMap);
intent.putExtras(extras);
and in second Activity
在第二个活动中
Bundle bundle = this.getIntent().getExtras();
if(bundle != null) {
hashMap = bundle.getSerializable("HashMap");
}
because Hashmapby default implements Serializable
so you can pass it using putSerializable
in Bundle and get in other activity using getSerializable
因为Hashmap默认实现,Serializable
所以你可以putSerializable
在 Bundle 中使用它传递它并使用getSerializable
回答by AMerle
回答by Martin Pfeffer
Please note: If you are using a AppCompatActivity, you will have to call the
protected void onSaveInstanceState(Bundle outState) {}
(NOTpublic void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {}
) method.
请注意:如果您使用的是 AppCompatActivity,则必须调用
protected void onSaveInstanceState(Bundle outState) {}
( NOTpublic void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {}
) 方法。
Example code...
示例代码...
Store the map:
存储地图:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("leftMaxima", leftMaxima);
outState.putSerializable("rightMaxima", rightMaxima);
}
And receive it in onCreate:
并在 onCreate 中接收它:
if (savedInstanceState != null) {
leftMaxima = (HashMap<Long, Float>) savedInstanceState.getSerializable("leftMaxima");
rightMaxima = (HashMap<Long, Float>) savedInstanceState.getSerializable("rightMaxima");
}
Sorry if it's some kind of a duplicate answer - maybe someone will find it useful. :)
对不起,如果这是某种重复的答案 - 也许有人会发现它很有用。:)
回答by Kapil Vats
If you want to send all the keys in the bundle, you can try
如果您想发送捆绑包中的所有密钥,您可以尝试
for(String key: map.keySet()){
bundle.putStringExtra(key, map.get(key));
}
回答by Jasss
public static Bundle mapToBundle(Map<String, Object> data) throws Exception {
Bundle bundle = new Bundle();
for (Map.Entry<String, Object> entry : data.entrySet()) {
if (entry.getValue() instanceof String)
bundle.putString(entry.getKey(), (String) entry.getValue());
else if (entry.getValue() instanceof Double) {
bundle.putDouble(entry.getKey(), ((Double) entry.getValue()));
} else if (entry.getValue() instanceof Integer) {
bundle.putInt(entry.getKey(), (Integer) entry.getValue());
} else if (entry.getValue() instanceof Float) {
bundle.putFloat(entry.getKey(), ((Float) entry.getValue()));
}
}
return bundle;
}
回答by Jocky Doe
I am using my kotlin implementation of Parcelable to achieve that and so far it works for me. It is useful if you want to avoid the heavy serializable.
我正在使用 Parcelable 的 kotlin 实现来实现这一点,到目前为止它对我有用。如果您想避免繁重的可序列化,这很有用。
Also in order for it to work, I recommend using it with these
同样为了让它工作,我建议将它与这些一起使用
Declaration
宣言
class ParcelableMap<K,V>(val map: MutableMap<K,V>) : Parcelable {
constructor(parcel: Parcel) : this(parcel.readMap(LinkedHashMap<K,V>()))
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeMap(map)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<ParcelableMap<Any?,Any?>> {
@JvmStatic
override fun createFromParcel(parcel: Parcel): ParcelableMap<Any?,Any?> {
return ParcelableMap(parcel)
}
@JvmStatic
override fun newArray(size: Int): Array<ParcelableMap<Any?,Any?>?> {
return arrayOfNulls(size)
}
}
}
Use
用
write
写
val map = LinkedHashMap<Int, String>()
val wrap = ParcelableMap<Int,String>(map)
Bundle().putParcelable("your_key", wrap)
read
读
val bundle = fragment.arguments ?: Bundle()
val wrap = bundle.getParcelable<ParcelableMap<Int,String>>("your_key")
val map = wrap.map
Don't forget that if your map K,V
are not parceled by default they must implement Parcelable
不要忘记,如果您的地图K,V
默认未打包,则必须实施Parcelable
回答by CoolMind
In Kotlin:
在科特林:
hashMap = savedInstanceState?.getSerializable(ARG_HASH_MAP) as? HashMap<Int, ValueClass>
putSerializable(ARG_HASH_MAP, hashMap)