• android页面间传递对象


    android传递对象有两种方式:

    一种是Serializable和Parcelable

    对于第一种方式:

    import java.io.Serializable;


    public class ShopData implements Serializable {
    public String shopId;
    public String name;
    public String url;
    public String info;


    public String getShopId() {
    return shopId;
    }


    public void setShopId(String shopId) {
    this.shopId = shopId;
    }


    public String getName() {
    return name;
    }


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


    public String getUrl() {
    return url;
    }


    public void setUrl(String url) {
    this.url = url;
    }


    public String getInfo() {
    return info;
    }


    public void setInfo(String info) {
    this.info = info;
    }


    }

    传递值的时候这样来传递:

    	<span style="white-space:pre">			</span>Intent intent = new Intent();
    				ShopData shopData = (ShopData)mAdapter.getItem(position);
    //				intent.putExtra("detail", shopData);
    				Bundle bundle = new Bundle();
    				bundle.putSerializable("shop", shopData);
    				intent.putExtra("detail", shopData);
    				intent.setClass(MainActivity.this, DetailShopActivity.class);
    				MainActivity.this.startActivity(intent);
    取值的时候:

    <span style="white-space:pre">		</span>Intent intent = getIntent();
    //		ShopData data = intent.getParcelableExtra("detail");
    		ShopData data = (ShopData)intent.getSerializableExtra("detail");

    对于另外一种方式:

    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class ShopData implements Parcelable{
    
    	public String shopId;
    	public String name;
    	public String url;
    	public String info;
    
    	public ShopData(Parcel in) {
    		readFromParcel(in);
    	}
    	
    	public ShopData() {
    	}
    	
    
    	public static final Parcelable.Creator<ShopData> CREATOR = new Parcelable.Creator<ShopData>() {
    		
    		public ShopData createFromParcel( Parcel in ){
    			return new ShopData(in);
    		}
    		
    		public ShopData[] newArray( int size){
    			return new ShopData[size];
    		}
    	};
    	
    	@Override
    	public int describeContents() {
    		return 0;
    	}
    
    	@Override
    	public void writeToParcel(Parcel dest, int flags) {
    		dest.writeString(shopId);
    		dest.writeString(name);
    		dest.writeString(url);
    		dest.writeString(info);
    		
    	}
    	public void readFromParcel( Parcel in ){
    		shopId = in.readString();
    		name = in.readString();
    		url = in.readString();
    		info = in.readString();
    	}
    	
    }

    传递的时候这样写:

    	<span style="white-space:pre">			</span>Intent intent = new Intent();
    				ShopData shopData = (ShopData)mAdapter.getItem(position);
    				intent.putExtra("detail", shopData);
    				intent.putExtra("detail", shopData);
    				intent.setClass(MainActivity.this, DetailShopActivity.class);
    				MainActivity.this.startActivity(intent);


    取值的时候:

    <span style="white-space:pre">		</span>Intent intent = getIntent();
    		ShopData data = intent.getParcelableExtra("detail");



  • 相关阅读:
    POJ2723 Get Luffy Out解题报告tarjan+2-SAT+二分
    poj2186Popular Cows+tarjan缩点+建图
    KMP模板
    洛谷P1939【模板】矩阵加速(数列)+矩阵快速幂
    矩阵快速幂模板
    codeforce#483div2D-XOR-pyramid+DP
    codeforce#483div2C-Finite or not?数论,GCD
    codeforce978C-Almost Arithmetic Progression+暴力,枚举前两个数字的情况
    codeforce440C-Maximum splitting-规律题
    LuoGu-P2863牛的舞会The Cow Prom[tarjan 缩点模板]
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6796304.html
Copyright © 2020-2023  润新知