韌館-LearnHouse

[Android]透過Parcelable在Activity間傳遞ArrayList物件

Android開發人員都知要傳遞物件給Activity,不能直接寫一個public的function來set資料
而要透過intent來夾帶傳遞的訊息,能傳遞的資料型態又不能是自己定義的object
別說自己定義的了,就連ArrayList,也無法隨意的塞進去intent來傳遞
只能是以下幾種:

Intent 	putExtra(String name, String[] value)
Intent 	putExtra(String name, Parcelable value)
Intent 	putExtra(String name, long value)
Intent 	putExtra(String name, boolean value)
Intent 	putExtra(String name, double value)
Intent 	putExtra(String name, Parcelable[] value)
Intent 	putExtra(String name, char value)
Intent 	putExtra(String name, int[] value)
Intent 	putExtra(String name, int value)
Intent 	putExtra(String name, double[] value)	 	 	 	
Intent 	putExtra(String name, short value) 	 	 	 	 	
Intent 	putExtra(String name, long[] value) 	 	 	 	 	
Intent 	putExtra(String name, boolean[] value) 	 	 	 	 	
Intent 	putExtra(String name, short[] value) 	 	 	 	 	
Intent 	putExtra(String name, String value) 	 	 	 	 	
Intent 	putExtra(String name, Serializable value) 	 	 	 	 	
Intent 	putExtra(String name, float[] value) 	 	 	 	 	
Intent 	putExtra(String name, Bundle value) 	 	 	 	 	
Intent 	putExtra(String name, byte[] value) 	 	 	 	 	
Intent 	putExtra(String name, CharSequence value) 	 	 	 	 	
Intent 	putExtra(String name, char[] value) 	 	 	 	 	
Intent 	putExtra(String name, byte value)

因此如果要用ArrayList來傳遞物件,則需先將要傳遞的物件轉成Parcelable

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

public class Module implements Parcelable {
	private String name;
	private String version;
	private String url;
	private String checksum;
	private String size;

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

	public String getName() {
	    return name;
	}
	
	public void setVersion(String version) {
	    this.version = version;
	}

	public String getVersion() {
	    return version;
	}
	
	public void setURL(String url) {
	    this.url = url;
	}

	public String getURL() {
	    return url;
	}
	
	public void setChecksum(String checksum) {
	    this.checksum = checksum;
	}

	public String getChecksum() {
	    return checksum;
	}
	
	public void setSize(String size) {
	    this.size = size;
	}

	public String getSize() {
	    return size;
	}

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

	@Override
	public void writeToParcel(Parcel out, int flags)
	{
		out.writeString(name);
		out.writeString(version);
		out.writeString(url);
		out.writeString(checksum);
		out.writeString(size);
	}

	public static final Parcelable.Creator<Module> CREATOR = new Creator<Module>() {
		@Override
		public Module[] newArray(int size) {
			return new Module[size];
		}

		@Override
		public Module createFromParcel(Parcel in) {
			return new Module(in);
		}
	};

	public Module(Parcel in) {
		name = in.readString();
		version = in.readString();
		url = in.readString();
		checksum = in.readString();
		size = in.readString();
	}
}

傳遞端:

ArrayList<Module> updateList;
Module module = new Module();
module.setName("aaa");
...(略)
updateList.add(module);

Intent intent = new Intent(context, TargetActivity.class);
intent.putParcelableArrayListExtra("updateList", updateList);
startActivity(intent);

Activity接收端:

ArrayList<Module> updateList = getIntent().getParcelableArrayListExtra("updateList");
for (int i=0; i<updateList.size(); i++) {
    Module module = (Module) updateList.get(i);
	...(略)
}
posted by admin in 程式&軟體 and have No Comments

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment