Here is my JSONArray Response from Web service:
And Class Java :
public class Product {
private int id,price,discount;
private String name,image,description,discount_type,discount_exp;
#SerializedName("products")
private List<Product> products;
public Product()
{
}
}
response is null
Your POJO class is wrong
Try this
public class Products_Main
{
#SerializedName("current_page")
int current_page;
#SerializedName("data")
private List<Product> products;
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
and
class Product {
#SerializedName("id")
private int id;
#SerializedName("price")
int price;
#SerializedName("discount")
int discount;
#SerializedName("name")
private String name;
#SerializedName("image")
private String image;
#SerializedName("description")
private String description;
#SerializedName("discount_type")
private String discount_type;
#SerializedName("discount_exp")
private String discount_exp;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
}
Any null data happens when the parsing failed.
This is the Java for the response you've shown
public class ProductResponse {
ProductPage products;
}
public class ProductPage {
int current_page;
#SerializedName("data")
private List<Product> products;
}
public class Product {
private int id,price,discount;
private String name,image,description,discount_type,discount_exp;
public Product() { }
}
You have to change like it will work
public class ProductModel {
private int current_page;
private ArrayList<DataModel> data = new ArrayList<>();
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public ArrayList<DataModel> getData() {
return data;
}
public void setData(ArrayList<DataModel> data) {
this.data = data;
}
private class DataModel{
private int id,price,discount,shop_id,vote_id,vedeo_id,status;
private String name,image,description,discount_type,discount_exp,created_at,updated_at;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public int getShop_id() {
return shop_id;
}
public void setShop_id(int shop_id) {
this.shop_id = shop_id;
}
public int getVote_id() {
return vote_id;
}
public void setVote_id(int vote_id) {
this.vote_id = vote_id;
}
public int getVedeo_id() {
return vedeo_id;
}
public void setVedeo_id(int vedeo_id) {
this.vedeo_id = vedeo_id;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}
}
The issue is that the products are contained within the data field of the JSON response. Retrofit doesn't know this so it can't deserialize the response into a Product array.
You should use it like that
=>Main API Model Class
public class APIResult {
#SerializedName("products")
private ProductModelClass products;
public ProductModelClass getProducts() {
return products;
}
public void setProducts(ProductModelClass products) {
this.products = products;
}
}
=>For data and current page model class
public class ProductModelClass {
#SerializedName("current_page")
private int current_page;
#SerializedName("data")
private ArrayList<ProductDataModel> data;
public int getCurrent_page() {
return current_page;
}
public void setCurrent_page(int current_page) {
this.current_page = current_page;
}
public ArrayList<ProductDataModel> getData() {
return data;
}
public void setData(ArrayList<ProductDataModel> data) {
this.data = data;
}
}
=>For Details of products Data
public class ProductDataModel {
#SerializedName("id")
private int id;
#SerializedName("name")
private String name;
#SerializedName("image")
private String image;
#SerializedName("description")
private String description;
#SerializedName("price")
private int price;
#SerializedName("discount")
private int discount;
#SerializedName("shop_id")
private int shop_id;
#SerializedName("discount_type")
private String discount_type;
#SerializedName("discount_exp")
private String discount_exp;
#SerializedName("discount_limit")
private String discount_limit;
#SerializedName("vote_id")
private String vote_id;
#SerializedName("video_id")
private String video_id;
#SerializedName("status")
private String status;
#SerializedName("created_at")
private String created_at;
#SerializedName("updated_at")
private String updated_at;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
public int getShop_id() {
return shop_id;
}
public void setShop_id(int shop_id) {
this.shop_id = shop_id;
}
public String getDiscount_type() {
return discount_type;
}
public void setDiscount_type(String discount_type) {
this.discount_type = discount_type;
}
public String getDiscount_exp() {
return discount_exp;
}
public void setDiscount_exp(String discount_exp) {
this.discount_exp = discount_exp;
}
public String getDiscount_limit() {
return discount_limit;
}
public void setDiscount_limit(String discount_limit) {
this.discount_limit = discount_limit;
}
public String getVote_id() {
return vote_id;
}
public void setVote_id(String vote_id) {
this.vote_id = vote_id;
}
public String getVideo_id() {
return video_id;
}
public void setVideo_id(String video_id) {
this.video_id = video_id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
}
Related
I am just creating a handler in java which accepts a POJO called as ProductsModel.
public ProductsResponseObject handleRequest(ProductsRequestObject productsRequestObject, Context arg1) {
ProductsResponseObject respObj = null;
ProductsService productsService = new ProductsService();
arg1.getLogger().log("inside ProductsHandler.handleRequest with reqtype " + productsRequestObject.getReqType());
switch (productsRequestObject.getReqType()) {
case 0:
respObj = productsService.addProduct(productsRequestObject);
break;
case 1:
respObj = productsService.updateProduct(productsRequestObject);
break;
default:
break;
}
return respObj;
}
But here inside the handler method, the attributes of request, reqType and productModel are null. Not sure y?
Request POJO:
public class ProductsRequestObject {
private int reqType;
private ProductsModel productsModel;
public int getReqType() {
return reqType;
}
public void setReqType(int reqType) {
this.reqType = reqType;
}
public ProductsModel getProductsModel() {
return productsModel;
}
public void setProductsModel(ProductsModel productsModel) {
this.productsModel = productsModel;
}
}
Model POJO:
public class ProductsModel {
private String id;
private String dealerMobNumber;
private String name;
private String description;
private String price;
#DynamoDBHashKey(attributeName = "Id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDealerMobNumber() {
return dealerMobNumber;
}
public void setDealerMobNumber(String dealerMobNumber) {
this.dealerMobNumber = dealerMobNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = 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;
}
This question already has an answer here:
How to convert JSONArray to List with Gson?
(1 answer)
Closed 6 years ago.
How to convert a jsonArray with multiple jsonObjects to an arraylist using Gson.
Here is my Json String:
[{"AccommoAddress":{
"PostalCode":2109,
"Street":"22 Ararat Str",
"Town":"Westdene"
},
"AccommoDetails":{
"AccommoId":0,
"CleaningService":1,
"DSTV":1,
"DedicatedStudyArea":1
},
"AccommoID":1,
"AccommoMainImage":{
"CategoryId":0,
"ContentType":".png",
"DateUploaded":"2016-07-16",
"FileSize":2362,
"ImageCategory":"MAIN",
"ImageId":1,
"ImageName":"images.png"
}]
First , it's not a correct Json format.
"ImageName":"images.png" <- (you need to put a "}" here )
Second , create a entity
public class TargetEntity {
private AccommoAddressEntity AccommoAddress;
private AccommoDetailsEntity AccommoDetails;
private int AccommoID;
private AccommoMainImageEntity AccommoMainImage;
public AccommoAddressEntity getAccommoAddress() {
return AccommoAddress;
}
public void setAccommoAddress(AccommoAddressEntity AccommoAddress) {
this.AccommoAddress = AccommoAddress;
}
public AccommoDetailsEntity getAccommoDetails() {
return AccommoDetails;
}
public void setAccommoDetails(AccommoDetailsEntity AccommoDetails) {
this.AccommoDetails = AccommoDetails;
}
public int getAccommoID() {
return AccommoID;
}
public void setAccommoID(int AccommoID) {
this.AccommoID = AccommoID;
}
public AccommoMainImageEntity getAccommoMainImage() {
return AccommoMainImage;
}
public void setAccommoMainImage(AccommoMainImageEntity AccommoMainImage) {
this.AccommoMainImage = AccommoMainImage;
}
public static class AccommoAddressEntity {
private int PostalCode;
private String Street;
private String Town;
public int getPostalCode() {
return PostalCode;
}
public void setPostalCode(int PostalCode) {
this.PostalCode = PostalCode;
}
public String getStreet() {
return Street;
}
public void setStreet(String Street) {
this.Street = Street;
}
public String getTown() {
return Town;
}
public void setTown(String Town) {
this.Town = Town;
}
}
public static class AccommoDetailsEntity {
private int AccommoId;
private int CleaningService;
private int DSTV;
private int DedicatedStudyArea;
public int getAccommoId() {
return AccommoId;
}
public void setAccommoId(int AccommoId) {
this.AccommoId = AccommoId;
}
public int getCleaningService() {
return CleaningService;
}
public void setCleaningService(int CleaningService) {
this.CleaningService = CleaningService;
}
public int getDSTV() {
return DSTV;
}
public void setDSTV(int DSTV) {
this.DSTV = DSTV;
}
public int getDedicatedStudyArea() {
return DedicatedStudyArea;
}
public void setDedicatedStudyArea(int DedicatedStudyArea) {
this.DedicatedStudyArea = DedicatedStudyArea;
}
}
public static class AccommoMainImageEntity {
private int CategoryId;
private String ContentType;
private String DateUploaded;
private int FileSize;
private String ImageCategory;
private int ImageId;
private String ImageName;
public int getCategoryId() {
return CategoryId;
}
public void setCategoryId(int CategoryId) {
this.CategoryId = CategoryId;
}
public String getContentType() {
return ContentType;
}
public void setContentType(String ContentType) {
this.ContentType = ContentType;
}
public String getDateUploaded() {
return DateUploaded;
}
public void setDateUploaded(String DateUploaded) {
this.DateUploaded = DateUploaded;
}
public int getFileSize() {
return FileSize;
}
public void setFileSize(int FileSize) {
this.FileSize = FileSize;
}
public String getImageCategory() {
return ImageCategory;
}
public void setImageCategory(String ImageCategory) {
this.ImageCategory = ImageCategory;
}
public int getImageId() {
return ImageId;
}
public void setImageId(int ImageId) {
this.ImageId = ImageId;
}
public String getImageName() {
return ImageName;
}
public void setImageName(String ImageName) {
this.ImageName = ImageName;
}
}
}
Third
Gson gson = new Gson();
TargetEntity[] target = gson.fromJson(JsonString,TargetEntity[].class)
I got a quite weird problem, forgive me if it became an attention problem, but I'm running on coffee right now!
I have this Json:
[
{
"address":"RS 239, Km 18,2 nÂș 4631 - Novo Hamburgo",
"closingTime":"06:00",
"description":"Curta como quiser.",
"distance":"6,328.35 km",
"iconUrl":"~\/Images\/Establishment\/Bar Alternativo.png",
"idEstablishment":5,
"name":"Bar Alternativo",
"openingTime":"22:30",
"phone":"(51) 3778-1820",
"type":"Casa Noturna \/ Balada"
}
]
And when I try deserialize this one using this code:
public static ArrayList<Establishment> serializeEstablishmentList(String json) {
ObjectMapper mapper = new ObjectMapper();
ArrayList<Establishment> establishments = null;
try {
establishments = mapper.readValue(json, new TypeReference<List<Establishment>>(){});
} catch (IOException e) {
e.printStackTrace();
}
return establishments;
}
My distance property do not get its value, image from debugger:
here goes my Establishment class:
public class Establishment {
private long idEstablishment;
private Drawable icon;
private String iconUrl;
private String name;
private String type;
private boolean workingStatus;
private String openingTime;
private String closingTime;
private String distance;
private String phone;
private String description;
private String address;
public Establishment() {
}
public Establishment(long idEstablishment, Drawable icon, String iconUrl, String name, String type, boolean workingStatus, String openingTime, String closingTime, String distance, String phone, String description, String address) {
this.idEstablishment = idEstablishment;
this.icon = icon;
this.iconUrl = iconUrl;
this.name = name;
this.type = type;
this.workingStatus = workingStatus;
this.openingTime = openingTime;
this.closingTime = closingTime;
this.distance = distance;
this.phone = phone;
this.description = description;
this.address = address;
}
public Establishment(long id, Drawable icon, String name, boolean workingStatus, String openingTime,
String closingTime, String distance) {
this.idEstablishment = id;
this.icon = icon;
this.name = name;
this.workingStatus = workingStatus;
this.openingTime = openingTime;
this.closingTime = closingTime;
this.distance = distance;
}
public long getIdEstablishment() {
return idEstablishment;
}
public void setIdEstablishment(long idEstablishment) {
this.idEstablishment = idEstablishment;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getWorkingStatus() {
return workingStatus;
}
public String getWorkingStatusLabel(){
return workingStatus ? "Aberto" : "Fechado";
}
public void setWorkingStatus(boolean workingStatus) {
this.workingStatus = workingStatus;
}
public String getOpeningTime() {
return openingTime;
}
public void setOpeningTime(String openingTime) {
this.openingTime = openingTime;
}
public String getClosingTime() {
return closingTime;
}
public void setClosingTime(String closingTime) {
this.closingTime = closingTime;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = this.distance;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isWorkingStatus() {
return workingStatus;
}
public String getIconUrl() {
return iconUrl;
}
public void setIconUrl(String iconUrl) {
this.iconUrl = iconUrl;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Any idea?
Your setter method is wrong:
public void setDistance(String distance) {
this.distance = this.distance;
}
This should be:
public void setDistance(String distance) {
this.distance = distance;
}
I'm using Kundera to do the persistence in MongoDB. I can persist some documents in my collection, but everytime that I try to find a specific document by id, I get this error
java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.BasicDBList
com.impetus.kundera.persistence.AbstractEntityReader.findById(AbstractEntityReader.java:95)
com.impetus.client.mongodb.MongoEntityReader.findById(MongoEntityReader.java:72)
com.impetus.kundera.lifecycle.states.ManagedState.handleFind(ManagedState.java:com.impetus.kundera.graph.Node.find(Node.java:500)
com.impetus.kundera.persistence.PersistenceDelegator.find(PersistenceDelegator.java:225)
com.impetus.kundera.persistence.PersistenceDelegator.findById(PersistenceDelegator.java:174)
com.impetus.kundera.persistence.EntityManagerImpl.find(EntityManagerImpl.java:263)
My class definition is
package data.additional;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.google.common.collect.Lists;
#Entity
#Table(name = "reference", schema = "RcordsDB#pl_records")
public class Reference {
#Id
#Column(name="idReference")
private String idReference;
#Column(name="profile_id")
private String profile_id;
#Column(name="group_id")
private String group_id;
#Column(name="created")
private Date created;
#Column(name="last_modified")
private Date last_modified;
#Column(name="identifiers")
private List<String> identifiers = Lists.newArrayList();
#Column(name="abstractText")
private String abstractText;
#Column(name="tags")
private String tags;
#Column(name="type")
private String type;
#Column(name="source")
private String source;
#Column(name="title")
private String title;
#Column(name="title")
private List<String> authors = Lists.newArrayList();
#Column(name="year")
private Date year;
#Column(name="volume")
private String volume;
#Column(name="issue")
private String issue;
#Column(name="pages")
private String pages;
#Column(name="series")
private String series;
#Column(name="chapter")
private String chapter;
#Column(name="websites")
private String websites;
#Column(name="accesed")
private String accesed;
#Column(name="publisher")
private String publisher;
#Column(name="city")
private String city;
#Column(name="edition")
private String edition;
#Column(name="institution")
private String institution;
#Column(name="editors")
private List<String> editors = Lists.newArrayList();
#Column(name="keywords")
private List<String> keywords = Lists.newArrayList();
#Column(name="doi")
private String doi;
#Column(name="isbn")
private String isbn;
#Column(name="issn")
private String issn;
public String getIdReference() {
return idReference;
}
public void setIdReference(String idReference) {
this.idReference = idReference;
}
public String getProfile_id() {
return profile_id;
}
public void setProfile_id(String profile_id) {
this.profile_id = profile_id;
}
public String getGroup_id() {
return group_id;
}
public void setGroup_id(String group_id) {
this.group_id = group_id;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getLast_modified() {
return last_modified;
}
public void setLast_modified(Date last_modified) {
this.last_modified = last_modified;
}
public List<String> getIdentifiers() {
return identifiers;
}
public void setIdentifiers(List<String> identifiers) {
this.identifiers = identifiers;
}
public String getAbstractText() {
return abstractText;
}
public void setAbstractText(String abstractText) {
this.abstractText = abstractText;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getAuthors() {
return authors;
}
public void setAuthors(List<String> authors) {
this.authors = authors;
}
public Date getYear() {
return year;
}
public void setYear(Date year) {
this.year = year;
}
public String getVolume() {
return volume;
}
public void setVolume(String volume) {
this.volume = volume;
}
public String getIssue() {
return issue;
}
public void setIssue(String issue) {
this.issue = issue;
}
public String getPages() {
return pages;
}
public void setPages(String pages) {
this.pages = pages;
}
public String getSeries() {
return series;
}
public void setSeries(String series) {
this.series = series;
}
public String getChapter() {
return chapter;
}
public void setChapter(String chapter) {
this.chapter = chapter;
}
public String getWebsites() {
return websites;
}
public void setWebsites(String websites) {
this.websites = websites;
}
public String getAccesed() {
return accesed;
}
public void setAccesed(String accesed) {
this.accesed = accesed;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
public String getInstitution() {
return institution;
}
public void setInstitution(String institution) {
this.institution = institution;
}
public List<String> getEditors() {
return editors;
}
public void setEditors(List<String> editors) {
this.editors = editors;
}
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
public String getDoi() {
return doi;
}
public void setDoi(String doi) {
this.doi = doi;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getIssn() {
return issn;
}
public void setIssn(String issn) {
this.issn = issn;
}
}
There is no error from Kundera side. The error is in the Entity class. You have given same Column Name to these two fields.
#Column(name="title")
private List<String> authors = Lists.newArrayList();
#Column(name="title")
private String title;
Update that name & your code will be working fine.
I have two classes which implement Parcelable. And objects of one of them I have to write to a list. First model:
public class Challenge implements Parcelable {
private long id;
#JsonProperty("max_volume")
private int maxVolume;
#JsonProperty("current_volume")
private int currentVolume;
private String name;
private String description;
#JsonProperty("max_count")
private int maxCount;
#JsonProperty("date_from")
private Date dateFrom;
#JsonProperty("date_to")
private Date dateTo;
#JsonProperty("create_time")
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
private Date createTime;
private ChallengeStatus status;
#JsonProperty("update_date")
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
private Date updateTime;
private ChallengeCategory category;
private Subject subject;
private ArrayList<User> userArrayList;
private int practice;
public double getProgress() {
return progress;
}
public void setProgress(double progress) {
this.progress = progress;
}
public int getPractice() {
return practice;
}
public void setPractice(int practice) {
this.practice = practice;
}
private double progress;
public Challenge() {
}
private Creator<User> creator;
protected Challenge(Parcel in) {
id = in.readLong();
maxVolume = in.readInt();
currentVolume = in.readInt();
name = in.readString();
description = in.readString();
maxCount = in.readInt();
// dateFrom = new Date(in.readLong());
// dateTo = new Date(in.readLong());
// createTime = new Date(in.readLong());
// status = ChallengeStatus.getEnum(in.readString());
// updateTime = new Date(in.readLong());
// category = ChallengeCategory.getEnum(in.readString());
subject = in.readParcelable(Subject.class.getClassLoader());
userArrayList = in.readTypedList(userArrayList, User.CREATOR);
}
public static final Creator<Challenge> CREATOR = new Creator<Challenge>() {
#Override
public Challenge createFromParcel(Parcel in) {
return new Challenge(in);
}
#Override
public Challenge[] newArray(int size) {
return new Challenge[size];
}
};
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getMaxVolume() {
return maxVolume;
}
public void setMaxVolume(int maxVolume) {
this.maxVolume = maxVolume;
}
public int getCurrentVolume() {
return currentVolume;
}
public void setCurrentVolume(int currentVolume) {
this.currentVolume = currentVolume;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getMaxCount() {
return maxCount;
}
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}
public Date getDateFrom() {
return dateFrom;
}
public void setDateFrom(Date dateFrom) {
this.dateFrom = dateFrom;
}
public Date getDateTo() {
return dateTo;
}
public void setDateTo(Date dateTo) {
this.dateTo = dateTo;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public ChallengeStatus getStatus() {
return status;
}
public void setStatus(ChallengeStatus status) {
this.status = status;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public ChallengeCategory getCategory() {
return category;
}
public void setCategory(ChallengeCategory category) {
this.category = category;
}
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
#Override
public int describeContents() {
return 0;
}
public ArrayList<User> getUserArrayList() {
return userArrayList;
}
public void setUserArrayList(ArrayList<User> userArrayList) {
this.userArrayList = userArrayList;
}
public static Creator<Challenge> getCREATOR() {
return CREATOR;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
dest.writeInt(maxVolume);
dest.writeInt(currentVolume);
dest.writeString(name);
dest.writeString(description);
dest.writeInt(maxCount);
// dest.writeLong(dateFrom.getTime());
// dest.writeLong(dateTo.getTime());
// dest.writeLong(createTime.getTime());
// dest.writeString(status.toString());
// dest.writeLong(updateTime.getTime());
// dest.writeString(category.toString());
dest.writeParcelable(subject, flags);
dest.writeTypedList(userArrayList);
}
}
And I have a problem at this line: userArrayList = in.readTypedList(userArrayList, User.CREATOR); It looks like this: And example of the second model:
public class User implements Parcelable {
long id;
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm ZZZZ")
Date time;
String image_src;
String first_name;
String last_name;
public User() {
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getImage_src() {
return image_src;
}
public void setImage_src(String image_src) {
this.image_src = image_src;
}
protected User(Parcel parcel) {
id = parcel.readLong();
time = new Date(parcel.readLong());
image_src = parcel.readString();
first_name = parcel.readString();
last_name = parcel.readString();
}
public static final Creator<User> CREATOR = new Creator<User>() {
#Override
public User createFromParcel(Parcel in) {
return new User(in);
}
#Override
public User[] newArray(int size) {
return new User[size];
}
};
public static Creator<User> getCREATOR() {
return CREATOR;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeLong(id);
// parcel.writeLong(time.getTime());
parcel.writeString(image_src);
parcel.writeString(first_name);
parcel.writeString(last_name);
}
}
Can you help me to find my mistake?
You must initialized userArrayList before use it:
userArrayList = new ArrayList<User>();
in.readTypedList(userArrayList, User.CREATOR);
Initialize ArraList and retrieve the data to this list from parcelable.
Something like this:
userArrayList = new ArrayList<User>();
in.readTypedList(userArrayList, User.CREATOR);