Activity中通過Intent傳遞HashMap集合
阿新 • • 發佈:2018-12-14
對於物件在Activity中的傳遞我們仍然可以使用Intent來實現,只是要在傳遞之前需要將Map強制轉化為Serialzable型別(對於自定義的類我們也可以讓其實現Serializable介面)來實現。
傳遞
private Map<Integer,String> mSelectBallMap = new HashMap<>(); Intent intent = new Intent(this,xxxActivity.class); intent.putExtra("map",(Serializable)mSelectBallMap); startActivity(intent);
接收
HashMap<Integer, String> map = (HashMap<Integer, String>) getIntent().getSerializableExtra("map");
for (Integer key : map.keySet()) {
Log.v("wjw","接收到的Key = " + key);
Log.v("wjw","接收到的value = " + map.get(key));
}