Android Intent传递对象和ArrayList

以前做项目的时候,需要Android Intent传递复杂类型,例如:数组,ArrayList类型,传递类对象,今天抽空写了个例子,方便以后用的时候查询!

程序主界面:

程序主界面

 

点击list按钮传递数据:

点击list按钮传递数据

 

点击parcelable传递数据:

点击parcelable传递数据

 

点击serializable传递数据:

点击serializable传递数据

下面看代码:

一、MainAcitivty.java类的代码:

package com.cn.daming;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements Serializable {

	private static final long serialVersionUID = 1L;

	private String s_name;
	private int s_number;
	private String s_sex;

	private Button list_Button;
	private Button ser_Button;
	private Button par_Button;
	private ArrayList m_list;

	public final static String PAR_KEY = "com.cn.daming.parcelable";
	public final static String SER_KEY = "com.cn.daming.serializable";
	public final static String LIST_KEY = "com.cn.daming.ArrayList";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initlist();
		drawBackground();
		initList_Button();
		initPar_Button();
		inintSer_Button();
	}

	public void initlist() {
		m_list = new ArrayList();
		m_list.add("大明ArrayList");
		m_list.add("年龄:25岁");
		m_list.add("性别:男");
	}

	public void drawBackground() {
		GradientDrawable grad = new GradientDrawable(Orientation.TL_BR,
				new int[] { Color.rgb(0, 0, 127), Color.rgb(0, 0, 255),
						Color.rgb(127, 0, 255), Color.rgb(127, 127, 255),
						Color.rgb(127, 255, 255), Color.rgb(255, 255, 255) });

		this.getWindow().setBackgroundDrawable(grad);
	}

	public void initList_Button() {
		list_Button = (Button) findViewById(R.id.list_button);
		list_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent list_intent = new Intent();
				list_intent.putStringArrayListExtra(LIST_KEY, m_list);
				list_intent.setClass(MainActivity.this, ShowListView.class);
				startActivity(list_intent);
			}
		});
	}

	public void initPar_Button() {
		par_Button = (Button) findViewById(R.id.par_button);
		par_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Student m_Student = new Student();
				m_Student.setName("大明例子");
				m_Student.setAge(25);
				m_Student.setSex("男");
				Intent p_Intent = new Intent(MainActivity.this,
						ShowParView.class);
				Bundle mBundle = new Bundle();
				mBundle.putParcelable(PAR_KEY, m_Student);
				p_Intent.putExtras(mBundle);
				startActivity(p_Intent);
			}
		});
	}

	public void inintSer_Button() {
		ser_Button = (Button) findViewById(R.id.ser_button);
		ser_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				MainActivity s_activity = new MainActivity();
				s_activity.setS_name("Daming Serlizable!");
				s_activity.setS_number(25);
				s_activity.setS_sex("男");
				Intent mIntent = new Intent(MainActivity.this,
						ShowSerView.class);
				Bundle mBundle = new Bundle();
				mBundle.putInt("state", 3);
				mBundle.putSerializable(SER_KEY, s_activity);
				mIntent.putExtras(mBundle);
				startActivity(mIntent);
			}
		});
	}

	public void setS_name(String s_name) {
		this.s_name = s_name;
	}

	public String getS_name() {
		return s_name;
	}

	public void setS_number(int s_number) {
		this.s_number = s_number;
	}

	public int getS_number() {
		return s_number;
	}

	public void setS_sex(String s_sex) {
		this.s_sex = s_sex;
	}

	public String getS_sex() {
		return s_sex;
	}
}

二、Student.java类的代码:

package com.cn.daming;

import android.os.Parcel;
import android.os.Parcelable;

public class Student implements Parcelable {

	private String name;
	private int age;
	private String sex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public static final Parcelable.Creator CREATOR = new Creator() {
		public Student createFromParcel(Parcel source) {
			Student mStudent = new Student();
			mStudent.name = source.readString();
			mStudent.age = source.readInt();
			mStudent.sex = source.readString();
			return mStudent;
		}

		public Student[] newArray(int size) {
			return new Student[size];
		}
	};

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel parcel, int arg1) {
		parcel.writeString(name);
		parcel.writeInt(age);
		parcel.writeString(sex);
	}
}

三、ShowListView.java类的代码:

package com.cn.daming;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.widget.TextView;

public class ShowListView extends Activity {

	private Intent list_intent;
	private ArrayList m_arrayList;
	private TextView list_textview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_list_view);
		drawBackground();
		list_textview = (TextView) findViewById(R.id.list_text_view);
		list_intent = getIntent();
		m_arrayList = list_intent.getExtras().getStringArrayList(
				MainActivity.LIST_KEY);
		m_arrayList.get(0);
		list_textview.setText(m_arrayList.get(0) + " 
" + m_arrayList.get(1)
				+ "
" + m_arrayList.get(2));
	}

	public void drawBackground() {
		GradientDrawable grad = new GradientDrawable(Orientation.TL_BR,
				new int[] { Color.rgb(0, 0, 127), Color.rgb(0, 0, 255),
						Color.rgb(127, 0, 255), Color.rgb(127, 127, 255),
						Color.rgb(127, 255, 255), Color.rgb(255, 255, 255) });

		this.getWindow().setBackgroundDrawable(grad);
	}
}

四、ShowParView.java类的代码:

package com.cn.daming;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.widget.TextView;

public class ShowParView extends Activity {

	private TextView par_text_view;

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

		par_text_view = (TextView) findViewById(R.id.par_text_view);
		Student p_student = (Student) getIntent().getParcelableExtra(
				MainActivity.PAR_KEY);
		par_text_view.setText("姓名: " + p_student.getName() + "
" + "年龄: "
				+ p_student.getAge() + "
" + "性别 : " + p_student.getSex()
				+ "
" + "类:" + p_student.getClass());

	}

	public void drawBackground() {
		GradientDrawable grad = new GradientDrawable(Orientation.TL_BR,
				new int[] { Color.rgb(0, 0, 127), Color.rgb(0, 0, 255),
						Color.rgb(127, 0, 255), Color.rgb(127, 127, 255),
						Color.rgb(127, 255, 255), Color.rgb(255, 255, 255) });

		this.getWindow().setBackgroundDrawable(grad);
	}
}

五、ShowSerView.java类的代码:

package com.cn.daming;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.widget.TextView;

public class ShowSerView extends Activity {
	private TextView ser_text_view;

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

		ser_text_view = (TextView) findViewById(R.id.ser_text_view);
		MainActivity s_activity = (MainActivity) getIntent()
				.getSerializableExtra(MainActivity.SER_KEY);
		ser_text_view.setText("You name is: " + s_activity.getS_name() + "
"
				+ "You age is: " + s_activity.getS_number() + "
"
				+ "You sex is: " + s_activity.getS_sex());
	}

	public void drawBackground() {
		GradientDrawable grad = new GradientDrawable(Orientation.TL_BR,
				new int[] { Color.rgb(0, 0, 127), Color.rgb(0, 0, 255),
						Color.rgb(127, 0, 255), Color.rgb(127, 127, 255),
						Color.rgb(127, 255, 255), Color.rgb(255, 255, 255) });

		this.getWindow().setBackgroundDrawable(grad);
	}
}

xml布局文件

一、main.xml布局文件:

二、show_list_view.xml布局文件:

三、show_par_view.xml布局文件:

四、show_ser_view.xml布局文件:

Manifest.xml布局文件:

暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心