Activitie之間傳對象,通過Parcelable(轉)
對象必須實現Serializable,對象代碼如下:
- import java.io.Serializable;
- import android.graphics.drawable.Drawable;
- /**
- * 包括軟件信息及評論
- * */
- public class MyApplicationInfo extends Object implements Serializable{
- /**
- *
- */
- private static final long serialVersionUID = -5151302508106116740L;
- //程序名稱
- private String title;
- //程序名稱
- private String label;
- //程序版本
- private String version;
- //新版本
- private String newVersion;
- //內容介紹
- private String content;
- //作者
- private String author;
- //圖標鏈接
- private String icon;
- //圖標
- private Drawable dicon;
- //演示圖片動態數組
- private String[] Demos;
- //價格
- private String price;
- //總共5星,以數字形式存儲
- private String star;
- //鏈接地址
- private String detailaddress;
- //評論包括:評論者、評論時間、評論內容、評論星級
- private Details details;
- //包名
- private String packageName;
- //啟動程序Class
- private String className;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getVersion() {
- return version;
- }
- public void setVersion(String version) {
- this.version = version;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public String getIcon() {
- return icon;
- }
- public void setIcon(String icon) {
- this.icon = icon;
- }
- public String getPrice() {
- return price;
- }
- public void setPrice(String price) {
- this.price = price;
- }
- public String getStar() {
- return star;
- }
- public void setStar(String star) {
- this.star = star;
- }
- public String getDetailaddress() {
- return detailaddress;
- }
- public void setDetailaddress(String detailaddress) {
- this.detailaddress = detailaddress;
- }
- public String[] getDemos() {
- return Demos;
- }
- public void setDemos(String[] demos) {
- Demos = demos;
- }
- public Details getDetails() {
- return details;
- }
- public void setDetails(Details details) {
- this.details = details;
- }
- public String getPackageName() {
- return packageName;
- }
- public void setPackageName(String packageName) {
- this.packageName = packageName;
- }
- public Drawable getDicon() {
- return dicon;
- }
- public void setDicon(Drawable dicon) {
- this.dicon = dicon;
- }
- public String getLabel() {
- return label;
- }
- public void setLabel(String label) {
- this.label = label;
- }
- public String getNewVersion() {
- return newVersion;
- }
- public void setNewVersion(String newVersion) {
- this.newVersion = newVersion;
- }
- public String getClassName() {
- return className;
- }
- public void setClassName(String className) {
- this.className = className;
- }
- }
自定義:AppParcelable
- import android.os.Parcel;
- import android.os.Parcelable;
- import com.tcad.marketassistant.vo.MyApplicationInfo;
- public class AppParcelable implements Parcelable {
- private MyApplicationInfo info;
- public AppParcelable(Parcel source){
- info = (MyApplicationInfo)source.readValue(MyApplicationInfo.class.getClassLoader());
- }
- public AppParcelable(MyApplicationInfo info){
- this.info = info;
- }
- public int describeContents() {
- return 0;
- }
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeValue(info);
- }
- public static final Parcelable.Creator<AppParcelable> CREATOR = new Parcelable.Creator<AppParcelable>() {
- public AppParcelable createFromParcel(Parcel source) {
- return new AppParcelable(source);
- }
- public AppParcelable[] newArray(int size) {
- // return new AppParcelable[size];
- throw new UnsupportedOperationException();
- }
- };
- public MyApplicationInfo getInfo(){
- return info;
- }
- }
調用代碼,發送:
- AppParcelable parcelable = new AppParcelable(info);
- //Info為MyApplicationInfo對象
- // 發送對象
- intent.putExtra("app_parcelable", parcelable);
- startActivity(intent);
接收:
- AppParcelable p = getIntent().getParcelableExtra("app_parcelable");
- MyApplicationInfo info = p.getInfo();
posted on 2010-09-10 16:00 九寶 閱讀(2142) 評論(0) 編輯 收藏 所屬分類: android