Android Activity跳转传递序列化实体类对象

       我发现自己真的好菜,想学点知识,可是不知道从何下手,今天一天状态确实很不好。实在无聊,写点以前遇到过的问题,方便以后用到时查看,更希望能与别人分享。

       本篇博客实现Android activity跳转的时候携带我自定义的实体类

       Bundle给我们提供了一个putParcelable(key,value)的方法。专门用于传递序列化对象。同样如果我们想传递一个list<实体类>,只要在bundle调用putParcelableArrayList(key, value)即可,当然list里的对象要序例化!

   话不多说,上代码:

package com.weiwend.wj.testactivity;

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

public class UserBean implements Parcelable{

	private String user_name;
	private int user_age;
	private int user_sex;
	private int user_addr;
	
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public int getUser_age() {
		return user_age;
	}
	public void setUser_age(int user_age) {
		this.user_age = user_age;
	}
	public int getUser_sex() {
		return user_sex;
	}
	public void setUser_sex(int user_sex) {
		this.user_sex = user_sex;
	}
	public int getUser_addr() {
		return user_addr;
	}
	public void setUser_addr(int user_addr) {
		this.user_addr = user_addr;
	}
	
	public static final Parcelable.Creator<UserBean> CREATOR=new Creator<UserBean>() {
		
		@Override
		public UserBean createFromParcel(Parcel source) {
			UserBean bean=new UserBean();
			
			bean.user_name=source.readString();
			bean.user_age=source.readInt();
			bean.user_sex=source.readInt();
			bean.user_addr=source.readInt();
			
			return bean;
		}
		
		@Override
		public UserBean[] newArray(int size) {
			// TODO Auto-generated method stub
			return new UserBean[size];
		}
	};
	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(user_name);
		dest.writeInt(user_age);
		dest.writeInt(user_sex);
		dest.writeInt(user_addr);
	}
	
	
	
}</span><span style="font-size:12px;">
</span>
可以看到自定义类实现了Parcelable接口, Parcelable需要实现writeToParcel、describeContents函数以及静态的CREATOR变量,实际上就是将如何打包和解包的工作自己来定义(这里我遇到过问题,就是变量写入的数序一定要跟读取的数序一样,否则程序会崩溃,实际上Parcelable底层就是按顺序转成二进制码存储,读取的),而序列化的这些操作完全由底层实现,其中CREATOR 用来创建自定义的Parcelable的对象.

接下来就是如何使用了:

MainActivity中,就放了一个button

public class MainActivity extends Activity {

	private Context context;
	
	private Button button;
	
	private UserBean userBean;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context=this;
		
		button=(Button) findViewById(R.id.btn);
		
		userBean=new UserBean();
		userBean.setUser_name("影魔");
		userBean.setUser_age(3);
		userBean.setUser_sex(1);
		userBean.setUser_addr("帝都");
		
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				Bundle bundle=new Bundle();
				bundle.putParcelable("user", userBean);
				intent.setClass(context, OtherActivity1.class);
				intent.putExtra("bundle", bundle);
				context.startActivity(intent);
			}
		});
	
	}

}
OtherActivity中,就放了一个textView

public class OtherActivity1 extends Activity {

	private TextView tv;
	
	private UserBean userBean;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_other1);
		
		tv=(TextView) findViewById(R.id.tv);
		
		Bundle bundle=(Bundle) getIntent().getExtras().get("bundle");
		userBean=(UserBean) bundle.getParcelable("user");
		
		tv.setText("name:"+userBean.getUser_name()+",age:"+userBean.getUser_age()+
				",sex:"+userBean.getUser_sex()+",addr:"+userBean.getUser_addr());
	}

}
再看一下运行效果吧。(哈哈,突出一个丑)
Android Activity跳转传递序列化实体类对象_第1张图片

你可能感兴趣的:(android,Activity传递实体)