码迷,mamicode.com
首页 > 其他好文 > 详细

Intent 传递对象和集合

时间:2016-05-12 15:02:44      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

两个Activity跳转的时候,相互之间传递对象数据,这种需求还是比较常见的,本文实现了在开启新Activity的同时传递对象参数。

如果对序列化和反序列化不太理解,可以参考这篇文章<java序列化和反序列化的简单理解>

首先是效果图:

技术分享

废话不多说直接展示代码

1.SendActivity.java的内容:


public class SendActivity extends Activity implements OnClickListener {

	
	Button btn_serializable;
	Button btn_parcelable;
	Button btn_sendList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        btn_serializable = (Button) findViewById(R.id.btn_serializable);
        btn_parcelable = (Button) findViewById(R.id.btn_parcelable);
        btn_sendList =  (Button) findViewById(R.id.btn_sendList);
        btn_serializable.setOnClickListener(this);
        btn_parcelable.setOnClickListener(this);
        btn_sendList.setOnClickListener(this);
    }
    
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_serializable:
			sendSerializableObject();
			break;
		case R.id.btn_parcelable:
			sendParcelableObject();
			break;
		case R.id.btn_sendList:
			sendSerializableList();
			break;
		default:
			break;
		}
	}
	
	private void sendSerializableObject(){
		Intent intent = new Intent(this,ReceiveSerActivity.class);
		UserBySerializable user = new UserBySerializable();
		user.setUserName("张三");
		user.setPassword(19940306);
//		Bundle bundle = new Bundle();
//		bundle.putSerializable("user", user);
//		intent.putExtras(bundle);
		intent.putExtra("user", user);
		startActivity(intent);
	}
	
	private void sendParcelableObject(){
		Intent intent = new Intent(this,ReceiveParActivity.class);
		UserByParcelable user = new UserByParcelable();
		user.setUserName("李四");
		user.setPassword(19930708);
//		Bundle bundle = new Bundle();
//		bundle.putParcelable("user", user);
//		intent.putExtras(bundle);
		intent.putExtra("user", user);
		startActivity(intent);	
	}
	
	private void sendSerializableList(){
		Intent intent = new Intent(this,ReceiveListActivity.class);
		List<UserBySerializable> list = new ArrayList<UserBySerializable>();
		UserBySerializable user01 = new UserBySerializable();
		user01.setUserName("一号成员");
		user01.setPassword(10000001);
		list.add(user01);
		UserBySerializable user02 = new UserBySerializable();
		user02.setUserName("二号成员");
		user02.setPassword(10000002);
		list.add(user02);
		UserBySerializable user03 = new UserBySerializable();
		user03.setUserName("三号成员");
		user03.setPassword(10000003);
		list.add(user03);
		intent.putExtra("userList", (Serializable)list);
		startActivity(intent);
	}
}

2.UserBySerializable对象:


public class UserBySerializable implements Serializable {
	private static final long serialVersionUID = -7060210544600464481L;
    private String userName;    
    private int password;    
    public void setUserName(String value)
    {
        userName = value;
    }
    public String getUserName()
    {
        return userName;
    }
	public int getPassword() {
		return password;
	}
	public void setPassword(int password) {
		this.password = password;
	}  
}

3.UserByParcelable对象


public class UserByParcelable implements Parcelable {
    private String userName;    
    private int password;    
    
    
    public void setUserName(String value)
    {
        userName = value;
    }
    public String getUserName()
    {
        return userName;
    }  
    
    public int getPassword() {
		return password;
	}
	public void setPassword(int password) {
		this.password = password;
	}

	public static final Parcelable.Creator<UserByParcelable> CREATOR = new Creator<UserByParcelable>() {  
        public UserByParcelable createFromParcel(Parcel source) {  
        	UserByParcelable mUser = new UserByParcelable();  
            mUser.userName = source.readString();  
            mUser.password = source.readInt();  
            return mUser;  
        }  
        public UserByParcelable[] newArray(int size) {  
            return new UserByParcelable[size];  
        }  
    };  
      
    public int describeContents() {  
        return 0;  
    }  
    public void writeToParcel(Parcel parcel, int flags) {  
        parcel.writeString(userName);  
        parcel.writeInt(password);  
    }    
}
这是最需要注意的三个部分,其余部分就不展示出来了,直接给大家Demo源码自己研究一下吧!点击打开链接

Intent 传递对象和集合

标签:

原文地址:http://blog.csdn.net/abc6368765/article/details/51362723

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