将可拆分对象传递给新活动将返回null

时间:2019-02-07 07:47:57

标签: java android list android-intent parcelable

我正在将可打包数据类的对象传递给用于简单字符串的活动 我可以把它放到另一个活动中,但是当我在对象中输入新的字符串列表时,该对象(现在包括一个列表)不会被传递到其他活动中。

这是我的数据类”

package com.example.user.shoppy.models;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class ProductModel implements Parcelable {

String product_name, description, price, currency, location, userId, image, 
product_Id,featuredImage;

List<String> imagesNames;

public ProductModel() {
}



// this constructor is getting all the string variables
public ProductModel(String product_Id, String product_name, String description, String price, String currency, String userId, String location) {
    this.product_name = product_name;
    this.description = description;
    this.price = price;
    this.currency = currency;
    this.location = location;
    this.userId = userId;
    this.product_Id = product_Id;
}
// this constructor is getting all the string variables but also an list 
public ProductModel(String product_id, String product_name, String des, String price, String currency, String userId, String location, String featuredImage, List imagesNames) {
    this.product_name = product_name;
    this.description = des;
    this.price = price;
    this.currency = currency;
    this.location = location;
    this.userId = userId;
    this.product_Id = product_id;
    this.featuredImage = featuredImage;
    this.imagesNames = imagesNames;
}


public String getProduct_Id() {
    return product_Id;
}

public void setProduct_Id(String product_Id) {
    this.product_Id = product_Id;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getProduct_name() {
    return product_name;
}

public void setProduct_name(String product_name) {
    this.product_name = product_name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getCurrency() {
    return currency;
}

public void setCurrency(String currency) {
    this.currency = currency;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getFeaturedImage() {        return featuredImage;    }

public void setFeaturedImage(String featuredImage) {        this.featuredImage = featuredImage;    }

public List<String> getImagesNames() {        return imagesNames;    }

public void setImagesNames(List<String> imagesNames) {        this.imagesNames = imagesNames;    }

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(description);
    dest.writeString(product_name);
    dest.writeString(price);
    dest.writeString(currency);
    dest.writeString(location);
    dest.writeString(userId);
    dest.writeString(image);
    dest.writeString(product_Id);
    dest.writeString(featuredImage);
    dest.writeStringList(imagesNames);
}

public static final Parcelable.Creator<ProductModel> CREATOR = new Creator<ProductModel>() {
    @Override
    public ProductModel createFromParcel(Parcel source) {
        return new ProductModel(source);
    }

    @Override
    public ProductModel[] newArray(int size) {
        return new ProductModel[size];
    }
};

private ProductModel(@NonNull Parcel in) {
    description = in.readString();
    product_name = in.readString();
    price = in.readString();
    currency = in.readString();
    location = in.readString();
    userId = in.readString();
    image = in.readString();
    product_Id = in.readString();
    featuredImage=in.readString();
    in.readList(imagesNames,String.class.getClassLoader());
}
}

这是剩余的代码:

  @Override
        public void onClick(View v) {

             Intent intent = new Intent(mContext, ProductSellingActivity.class);
            intent.putExtra("currentProductModel", currentProductModel);
            mContext.startActivity(intent);


        }
    });

现在我的意向培训班

 Intent intent=getIntent();
    currentProductModel= (ProductModel) intent.getParcelableExtra(ProductSellingActivity.INTENTNAME);

3 个答案:

答案 0 :(得分:0)

第二个活动密钥的接收意图应该相同。

Intent intent=getIntent();
currentProductModel= (ProductModel) intent.getParcelableExtra("currentProductModel");

尝试一下。

答案 1 :(得分:0)

我认为您的ProductModel没有在新的Activity中正确创建。

在您的包裹实施中尝试以下操作:

imagesNames = in.readArrayList(ProductModel.class.getClassLoader());

答案 2 :(得分:-1)

尝试这个;

Bundle bundle=new Bundle();
bundle.putParcelable("currentProductModel","currentProductModel");
intent.putExtras(bundle);

在其他活动中:

 Bundle bundle=this.getIntent().getExtras();
        CurrentProduct currentProduct=bundle.getParcelable("currentProductModel");