`
身心不坚强
  • 浏览: 36229 次
  • 性别: Icon_minigender_2
  • 来自: 西安
社区版块
存档分类
最新评论

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());
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics