Intent传递对象和集合的实现

注:对象需要实现Parcelable接口,并且重写三个方法

1.实体类:Device.java

package wlx.test;

import android.os.Parcel;
import android.os.Parcelable;
/**
* 注意写入和读出顺序要一致!!!
* @author Tracy.Lee
*
*/
public class Device implements Parcelable {
private String location;
private String name;

public Device() {

}

public Device(String location, String name) {
this.location = location;
this.name = name;
}

public String getName() {
return name;
}

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

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
// 先写位置,后写名称
dest.writeString(location);
dest.writeString(name);
}

public static final Creator<Device> CREATOR = new Creator<Device>() {
public Device createFromParcel(Parcel source) {
//先构造位置,后构造名称(与构造方法的顺序关联)
return new Device(source.readString(), source.readString());
}

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


2.传递对象:MainActivity.java

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* ******* 传递对象 OK********* */
Device device = new Device();
device.setLocation("SRoom");
device.setName("light");
intent.putExtra("device", device);
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});

3.取对象:OtherActivity.java

Intent intent = getIntent();
/* ******** 取对象 ******** */
Device device = intent.getParcelableExtra("device");
System.out.println("device location--->" + device.getLocation());
System.out.println("device name--->" + device.getName());

4.传递集合:MainActivity.java

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* ******* 传递集合 OK********* */
ArrayList<Device> devices = new ArrayList<Device>();
Device device = new Device();
device.setLocation("SRoom");
device.setName("light");
Device device1 = new Device();
device1.setLocation("ERoom");
device1.setName("certain");
devices.add(device);
devices.add(device1);
intent.putParcelableArrayListExtra("devices", devices);
intent.setClass(MainActivity.this, SRoomActivity.class);
startActivity(intent);
}
});

5.取集合:OtherActivity.java

Intent intent = getIntent();
/* ******** 取集合 ******** */
ArrayList<Device> devices = intent.getParcelableArrayListExtra("devices");
System.out.println("device1:location---->" + devices.get(0).getLocation());
System.out.println("device1:name---->" + devices.get(0).getName());
System.out.println("device2:location---->" + devices.get(1).getLocation());
System.out.println("device2:name---->" + devices.get(1).getName());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值