Android中activity之間如何傳遞Hashmap資料

來源:互聯網
上載者:User

標籤:

我的使用情境是當一個頁面需頁面中的listview中的hashmap的資料時要另一個,就要用到了傳遞hashmap資料。
在發送方,My Code是這樣的:

package cn.oddcloud.www.coffeestore.Test;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import java.util.HashMap;import cn.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=new HashMap<>();//定義一個map,用來儲存資料        map.put("to","我是分子");//索引值為to的放的值是;我是分子                                //   也分子        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {            //跳轉頁面                Intent intent=new Intent(Main2Activity.this,Main4Activity.class);                //讓hashmap實現可序列化則要定義一個實現可序列化的類。                SerMap serMap=new SerMap();                //傳遞map到SerMap 中的map,這樣資料就會傳遞到SerMap 中的map中。              serMap.setMap(map);              //建立Bundle對象,存放實現可序列化的SerMap                 Bundle bundle=new Bundle();                bundle.putSerializable("KEY",serMap);                //意圖放置bundle變數                intent.putExtras(bundle);                startActivity(intent);            }        });    }}

自訂類

package cn.oddcloud.www.coffeestore.Test;import java.io.Serializable;import java.util.HashMap;/** * Created by Administrator on 2016/5/8. */public class SerMap  implements Serializable {    public HashMap<String,Object> map;    public  SerMap(){    }    public HashMap<String, Object> getMap() {        return map;    }    public void setMap(HashMap<String, Object> map) {        this.map = map;    }}

接收方:

package cn.oddcloud.www.coffeestore.Test;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.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);        //獲得意圖        Intent intent=getIntent();        //得到資料集        Bundle bundle=intent.getExtras();        //獲得自訂類        SerMap serializableMap = (SerMap) bundle                .get("KEY");        map=serializableMap.getMap();        textView.setText(map.get("to").toString()+map.get("too").toString());    }}

Android中activity之間如何傳遞Hashmap資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.