[英]Android HashMap in Bundle?
The android.os.Message
uses a Bundle
to send with it's sendMessage-method. android.os.Message
使用Bundle
与sendMessage方法一起发送。 Therefore, is it possible to put a HashMap
inside a Bundle
? 因此,是否可以将
HashMap
放在Bundle
?
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 Hashmap by default implements Serializable
so you can pass it using putSerializable
in Bundle and get in other activity using getSerializable
因为哈希映射在默认情况下实现
Serializable
,所以你可以用它传递putSerializable
捆绑,并使用在其他活动获得getSerializable
Please note: If you are using a AppCompatActivity, you will have to call the protected void onSaveInstanceState(Bundle outState) {}
( NOT public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {}
) method. 请注意:如果您使用的是AppCompatActivity,则必须调用
protected void onSaveInstanceState(Bundle outState) {}
( NOT public 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. 对不起,如果它是某种重复的答案 - 也许有人会觉得它很有用。 :)
:)
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));
}
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;
}
I am using my kotlin implementation of Parcelable to achieve that and so far it works for me. 我正在使用我的kotlin Parcelable实现来实现这一点,到目前为止它对我有用。 It is useful if you want to avoid the heavy serializable.
如果您想避免繁重的可序列化,这很有用。
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
In Kotlin: 在Kotlin:
hashMap = savedInstanceState?.getSerializable(ARG_HASH_MAP) as? HashMap<Int, ValueClass>
putSerializable(ARG_HASH_MAP, hashMap)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.