Bundle类是一个key-value对,“A mapping from String values to various Parcelable types.”
两个activity之间的通讯可以通过bundle类来实现,实现步骤:
(1)新建一个bundle类
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)(3) 新建一个intent对象,并将该bundle加入这个intent对象
实现代码:
1.传递数据
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String, String> map = listData.get(position); Bundle bundle = new Bundle(); bundle.putSerializable("map", (Serializable)map); Intent intent = new Intent(Cyfwdw.this, Cyqyxx.class); intent.putExtra("bundle", bundle); startActivity(intent); finish(); } }2.接收数据
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cyqyxx_layout); Bundle bundle =getIntent().getExtras().getBundle("bundle"); HashMap<String,String> map = (HashMap<String,String>) bundle.getSerializable("map"); jc_dwmc.setText(String.valueOf(map.get("qymc"))); jc_dwdz.setText(String.valueOf(map.get("zcdz"))); jc_xkzh.setText(String.valueOf(map.get("zsbh"))); jc_yxqx.setText(String.valueOf(map.get("yxqz")));