码迷,mamicode.com
首页 > 移动开发 > 详细

Android Intent传递对象小结

时间:2014-06-24 14:50:59      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:android   style   class   blog   code   java   

效果:

bubuko.com,布布扣bubuko.com,布布扣

bubuko.com,布布扣bubuko.com,布布扣

bubuko.com,布布扣

想看实例的,感兴趣的能够看之前的一篇文章

Android ViewPager Fragment实现选项卡


部分关键代码:

public class SerializeActivity extends Activity implements Serializable {

	Button btnlist, btnParcelable, btnSerialze;
	private final String TAG = "SerializeActivity";
	public final static String LIST_KEY = "ArrayList";
	public final static String PAR_KEY = "parcelable";
	public final static String SER_KEY = "serializable";
	private static final long serialVersionUID = 1L;


	private ArrayList<String> m_list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_serialize);
		initView();
		initlist();
	}
	/*
	 * 初始化视图
	 */
	public void initView() {
		btnlist = (Button) findViewById(R.id.btnlist);
		btnParcelable = (Button) findViewById(R.id.btnParcelable);
		btnSerialze = (Button) findViewById(R.id.btnSerialze);
		btnlist.setOnClickListener(btnlistener);
		btnParcelable.setOnClickListener(btnlistener);
		btnSerialze.setOnClickListener(btnlistener);
	}
	public void initlist() {
		
			m_list = new ArrayList<String>();
			m_list.add("www.88ios.com");
			m_list.add("Android移动开发");
			m_list.add("IOS开发入门");
		
	}
	/*
	 * button点击事件
	 */
	OnClickListener btnlistener = new OnClickListener() {

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnlist:

				intentList();
				break;
			case R.id.btnParcelable:

				intentParcelabe();
				break;
			case R.id.btnSerialze:

				intentShowSer();

				break;
			}
		}
	};
     	/*
	    * 
	    */
		public void intentList() {
			Intent list_intent = new Intent();
			list_intent.putStringArrayListExtra(LIST_KEY, m_list);
			list_intent.setClass(SerializeActivity.this, ShowInfo.class);
			startActivity(list_intent);
		}
		   /*
		    * 
		    */
		public void intentParcelabe() {
			Student m_Student = new Student();
			m_Student.setName("88ios.com");
			m_Student.setAge(25);
			m_Student.setSex("男");
			Intent p_Intent = new Intent(SerializeActivity.this,
					ShowParcelabe.class);
			Bundle mBundle = new Bundle();
			mBundle.putParcelable(PAR_KEY, m_Student);
			p_Intent.putExtras(mBundle);
			startActivity(p_Intent);
		}
		   /*
		    * 
		    */
		public void intentShowSer() {
			final SerializableMap myMap = new SerializableMap();

			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			Map<String, Object> maplist;
			Map<String, Object> paramMap4 = new HashMap<String, Object>();

			paramMap4.put("name", "洪生鹏");
			paramMap4.put("website", "www.88ios.com");
			paramMap4.put("address", "广州");
			myMap.setMap(paramMap4);

			SerializableMap serializableMap = (SerializableMap) myMap;
			maplist = serializableMap.getMap();
			list.add(maplist);

			Intent mIntent = new Intent(SerializeActivity.this, ShowSer.class);
			Bundle mBundle = new Bundle();

			mBundle.putSerializable("map", (Serializable) myMap);
		   //mBundle.putSerializable("map", (Serializable) list);

			mIntent.putExtras(mBundle);
			startActivity(mIntent);
		}


}
ShowInfo.java

public class ShowInfo extends Activity {

	private Intent list_intent;
	private ArrayList<String> m_arrayList;
	private TextView textview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_info);

		textview = (TextView) findViewById(R.id.textview);
		StringBuffer sb = new StringBuffer();

		list_intent = getIntent();
		m_arrayList = list_intent.getExtras().getStringArrayList(
				SerializeActivity.LIST_KEY);
		m_arrayList.get(0);

		sb.append(m_arrayList.get(0) + " \n" + m_arrayList.get(1) + "\n"
				+ m_arrayList.get(2));

		textview.setText(sb);
	}

}

ShowParcelabe.java

public class ShowParcelabe extends Activity {
	private TextView textview;  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_parcelabe);
		
		textview = (TextView)findViewById(R.id.textview);  
        Student p_student = (Student)getIntent().getParcelableExtra(SerializeActivity.PAR_KEY);    
        textview.setText("姓名: " + p_student.getName()+"\n"+    
                          "年龄: " + p_student.getAge() + "\n" +    
                          "性别 : " + p_student.getSex() + "\n" +  
                          "类:" + p_student.getClass());    
	}



}

ShowSer.java

public class ShowSer extends Activity {
	private TextView textview;
	StringBuilder sb = new StringBuilder();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_ser);
		textview = (TextView)findViewById(R.id.textview);  
		Map<String, Object> maplist;
		Bundle bundle = getIntent().getExtras();
		SerializableMap serializableMap = (SerializableMap) bundle.get("map");
		maplist = serializableMap.getMap();
		/*
		for (String k : maplist.keySet()) {
			Toast.makeText(this, "" + maplist.get(k), Toast.LENGTH_SHORT)
					.show();
		}
        */
		Set set = maplist.entrySet();
		Iterator it = set.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String key = (String) entry.getKey();
			String valu = (String) entry.getValue();
			sb.append(key+"  "+valu+"\n");
		}
		textview.setText(sb);   
	}

}



转载请保留链接

http://hongshengpeng.com/article/show/271.aspx


源码下载 


QQ

交流群:154950206 进群验证:88ios


Android Intent传递对象小结,布布扣,bubuko.com

Android Intent传递对象小结

标签:android   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/mfrbuaa/p/3805852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!