目的: Activity间传递ArrayList<HashMap<String,String>> 方法: 1 利用bundle.putSerializable("arrayList",arrayList)添加欲传送的ArrayList 2 利用intent.putExtras(bundle)添加Bundle 3 利用startActivity(intent)启动下一个Activity //跳转前Activity package cn.com.brave; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class TestArrayListHashMapActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button=(Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Map<String, String> map1 = new HashMap<String, String>(); map1.put("name", "亮哥"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("age", "25"); Map<String, String> map3 = new HashMap<String, String>(); map3.put("trait", "帅气"); ArrayList<Map<String, String>> arrayList = new ArrayList<Map<String, String>>(); arrayList.add(map1); arrayList.add(map2); arrayList.add(map3); Bundle bundle = new Bundle(); bundle.putSerializable("arrayList", arrayList); Intent intent = new Intent(TestArrayListHashMapActivity.this, SecondActivity.class); intent.putExtras(bundle); startActivity(intent); } }); } } //跳转后Activity package cn.com.brave; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class SecondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); Bundle buldle = intent.getExtras(); ArrayList<HashMap<String, String>> list =((ArrayList<HashMap<String, String>>) buldle.getSerializable("arrayList")); for(Iterator<HashMap<String, String>> it=list.iterator();it.hasNext();){ HashMap<String, String> tempMap=it.next(); Set<String> set=tempMap.keySet(); for(Iterator<String> ito=set.iterator();ito.hasNext();){ String s=ito.next(); System.out.println(s+"="+tempMap.get(s)); } } Toast.makeText(this, "跳转成功", 0).show(); } } //备注: //布局文件很简单,只有一个Button.在此略去main.xml文件
时间: 2024-11-13 08:57:28