0

I have a class Tag Model and i need to send List< TagModel> to another activity. If i implement parcelable then I have another custom object array list in its items. How to solve this problem?

public class TagModel {

    int listing_count;
    ArrayList<ListingsModel> listingsModel;  //how to handle this list
    String foodTypeName;

    public int getListing_count() {
        return listing_count;
    }

    public void setListing_count(int listing_count) {
        this.listing_count = listing_count;
    }

    public String getFoodTypeName() {
        return foodTypeName;
    }

    public void setFoodTypeName(String foodTypeName) {
        this.foodTypeName = foodTypeName;
    }

    public ArrayList<ListingsModel> getListings() {
        return listingsModel;
    }

    public void setListings(ArrayList<ListingsModel> listingsModel) {
        this.listingsModel = listingsModel;

    }
}
Abhay Sood
  • 480
  • 2
  • 6
  • 20

1 Answers1

3

You 2 objects have to be Parceable to send.

I'm assuming your object "ListingsModel" is already parceable.

TagModel Parceable

public class TagModel implements Parcelable {

int listing_count;
ArrayList<ListingsModel> listingsModel;  //how to handle this list
String foodTypeName;

public int getListing_count() {
    return listing_count;
}

public void setListing_count(int listing_count) {
    this.listing_count = listing_count;
}

public String getFoodTypeName() {
    return foodTypeName;
}

public void setFoodTypeName(String foodTypeName) {
    this.foodTypeName = foodTypeName;
}

public ArrayList<ListingsModel> getListings() {
    return listingsModel;
}

public void setListings(ArrayList<ListingsModel> listingsModel) {
    this.listingsModel = listingsModel;

}

protected TagModel(Parcel in) {
    listing_count = in.readInt();
    if (in.readByte() == 0x01) {
        listingsModel = new ArrayList<ListingsModel>();
        in.readList(listingsModel, ListingsModel.class.getClassLoader());
    } else {
        listingsModel = null;
    }
    foodTypeName = in.readString();
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(listing_count);
    if (listingsModel == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(listingsModel);
    }
    dest.writeString(foodTypeName);
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<TagModel> CREATOR = new Parcelable.Creator<TagModel>() {
    @Override
    public TagModel createFromParcel(Parcel in) {
        return new TagModel(in);
    }

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

For parcelable object I recommend using this great tool parcelabler

joselufo
  • 3,393
  • 3
  • 23
  • 37