简体   繁体   中英

Android getParcelableExtra returns null

I am currently trying to pass some data from my fragment to an activity. I am passing some parameter, and also an arraylist of a model class. I already make sure that the arraylist before I passed it is not null, since I am able to print out the value in log. But it become null in the called activity.

Here is the model class:

public class Product implements Parcelable {

String code, id, status, is_expired;

public Product() {}

public Product(String code, String id, String status, String is_expired) {
    this.code = code;
    this.id = id;
    this.status = status;
    this.is_expired = is_expired;
}

public Product(Parcel in) {
    code = in.readString();
    id = in.readString();
    status = in.readString();
    is_expired = in.readString();
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getIs_expired() {
    return is_expired;
}

public void setIs_expired(String is_expired) {
    this.is_expired = is_expired;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(code);
    dest.writeString(id);
    dest.writeString(status);
    dest.writeString(is_expired);
}

public static final Parcelable.Creator<Product> CREATOR = new Parcelable.Creator<Product>() {
    public Product createFromParcel(Parcel in) {
        return new Product(in);
    }

    public Product[] newArray(int size) {
        return new Product[size];
    }
};
}

Here is the caller:

intent.putExtra("product", productList);

Here is how retrieve the data:

ArrayList<Product> productList = getIntent().getParcelableExtra("product");

What am I doing wrong here?

Try this .

intent.putParcelableArrayListExtra("product", productList);

And next Activity .

ArrayList<Product> productList = getIntent().getParcelableArrayListExtra("product");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM