注:对象需要实现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());
分享到:
相关推荐
Intent传递对象、对象数组
android Intent传递对象数据 通过对象分别实现Serializable Parcelable接口的两种方法
intent 传递对象,主要是通过intent传递自定义对象
Intent传递数据时,如果数据太大,可能会出现异常。比如App闪退,或是Intent发送不成功,logcat报错等等。 这就牵涉到一个问题:Intent 传递数据大小限制。 Intent到底能够携带多少数据呢? 使用Intent传送数据时...
Intent传递数据是android开发中最长用的数据传递方式,可是要传递对象不怎么常用,这里介绍第一种传递对象的方法Serializable传递
Xamarin.Android之Intent传递对象简单实例,实现的是IParcelable接口
被传递的对象需要先实现序列化,而序列化对象有两种方式:java.io.Serializable和android.os.Parcelable Java中使用的是Serializable,而谷歌在Android使用了自定义的Parcelable。 两种序列化方式的区别: 1.在...
使用intent传递对象,通过实现接口
本文实例讲述了android中intent传递list或者对象的方法。分享给大家供大家参考。具体实现方法如下: 方法一: 如果单纯的传递List<String> 或者List的话 就可以直接使用 代码如下: 代码如下:intent....
Activity 间intent传递有序排序的map集合 intent传递map排序
在Android中通过Intent使用Bundle传递对象源码
【Intent传递】对象、数组的DEMO,亲测,可运行,有详细注释
Android intent 传递对象以及返回刷新。这个demo简单实现了activity之间传递对象以及对象列表。activity返回时,带数据返回,以及刷新activity。欢迎下载源码。
android中Intent传递对象的两种方法(Serializable,Parcelable) 简单而又详细的demo,欢迎下载
intent传递自定义对象,很简单,只需要强转成serializable型就可以
本文实例分析了Android编程使用Intent传递对象的方法。分享给大家供大家参考,具体如下: 之前的文章中,介绍过Intent的用法,比如启动活动,发送广播,启发服务等,并且可以使用Intent时传递一些数据。如下代码所示...
Intent在不同的组件中传递对象数据的应用非常普遍,大家都知道在intent传递对象的方法有两种:1、实现Serializable接口、2、实现Parcelable接口。 Android中Intent传递对象的两种方法Serializable,Parcelable请点击...
在Android中intent传递对象主要有2种方式分别是,Bundle.putSerializable(Key,Object)和Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了...