My use of the scene is when a page needs a page in the ListView in the HashMap data to the other, it will be used to pass HASHMAP data.
On the sender side, my code is this:
PackageCn.oddcloud.www.coffeestore.Test;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportJava.util.HashMap;ImportCN.ODDCLOUD.WWW.COFFEESTORE.R; Public class main2activity extends appcompatactivity {Hashmap<string,object> map; Button button;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.activity_main2); button= (Button) Findviewbyid (R.id.button); map=NewHashmap<> ();//define a map to store the dataMap.put ("to","I'm a molecule.");The value of the drop to the//key value is; I'm a molecule. //also moleculeButton.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {//Jump pageIntent intent=NewIntent (main2activity. This, Main4activity.class);//Let HashMap implement serializable to define a class that implements serializable. Sermap sermap=NewSermap ();//Pass map to the map in Sermap, and the data will be passed to the map in Sermap. Sermap.setmap (map);//Create bundle objects, store sermap that implement serializableBundle bundle=NewBundle (); Bundle.putserializable ("KEY", Sermap);//Intent to place bundle variablesIntent.putextras (bundle); StartActivity (Intent); } }); }}
Custom Classes
PackageCn.oddcloud.www.coffeestore.Test;Importjava.io.Serializable;ImportJava.util.HashMap;/** * Created by Administrator on 2016/5/8. * * Public class sermapimplements Serializable { PublicHashmap<string,object> map; Public Sermap(){ } PublicHashmap<string, object>Getmap() {returnMap } Public void Setmap(hashmap<string, object> map) { This. map = map; }}
Receiving Party:
Package CN. Oddcloud. www. Coffeestore. Test;Import Android. Content. Intent;Import Android. OS. Bundle;Import Android. Support. V7. App. Appcompatactivity;Import Android. Widgets. TextView;Import Java. Util. HashMap;Import CN. Oddcloud. www. Coffeestore. R;public class Main4activity extends Appcompatactivity {hashmap<string,object> map;TextView TextView;@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_MAIN4);textview= (TextView) Findviewbyid (R. ID. Tess);Get Intent Intent intent=getintent ();Get Data Set Bundle bundle=intent. Getextras();Get custom class Sermap Serializablemap = (SERMAP) bundle. Get("KEY");Map=serializablemap. Getmap();TextView. SetText(Map. Get("to"). toString() +map. Get("Too"). toString());}}
How HashMap data is passed between activity in Android