How HashMap data is passed between activity in Android

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.