I'm trying to send an object between two activities.
The order Object contains a list of item witch I implemented like this:
OrderItem Object:
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeParcelable(priceTableItem, flags);
dest.writeInt(qunatity);
dest.writeDouble(value); // quantity * unitValue
dest.writeDouble(discount);
}
protected OrderItem(Parcel in) {
id = in.readInt();
priceTableItem = in.readParcelable(PriceTableItem.class.getClassLoader());
quantity = in.readInt();
value = in.readDouble();
discount = in.readDouble();
}
At the PriceTableItem I have a situation where it can contais a product id or a "grade" id ("grade" is when the product have color and size) but never have both values.
so I implemented like this:
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeParcelable(priceTable, flags);
dest.writeValue(produto);
dest.writeValue(grade);
dest.writeDouble(unitPrice);
dest.writeByte((byte) (isActive ? 1 : 0));
}
protected PriceTableItem(Parcel in) {
id = in.readInt();
priceTable = in.readParcelable(PriceTable.class.getClassLoader());
product = (Product) in.readValue(Product.class.getClassLoader());
grade = (Grade) in.readValue(Grade.class.getClassLoader());
unitPrice = in.readDouble();
isactive = in.readByte() != 0;
}
The problem occur when I pass the order object from my OrderListActivity to OrderDetailActivity. It read all attributes before my list of item. When it try to read the PriceTable on OrderItem I get:
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.intelecto.intesigmobile/br.com.intelecto.intesigmobile.activity.PedidoDetailActivity}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling:
The problem line is:
priceTableItem = in.readParcelable(PriceTableItem.class.getClassLoader());
Any ideas on how to solve this?
Still don't know what caused the error, but I solved the problem like bellow
When I was passing the order value I was using only the Intent to do it, like this:
Intent i = new Intent(this, OrderDetailActivity.class);
i.putExtras("order", order);
startActivity(i);
And, to read it I was doing like this:
Order order = getIntent().getParcelableExtra("order");
So, I used a Bundle for pass the value.
Intent i = new Intent(this, OrderDetailActivity.class);
Bundle b = new Bundle();
b.putParcelable("order", order);
i.putExtras(b);
startActivity(i);
And
Bundle b = getIntent().getExtras();
Order order = b.getParcelable("order");
I had same error and the lines causing the error were these:
ingredients = in.readParcelable(Ingredient.class.getClassLoader());
and
public static final Parcelable.Creator<RecipeContent> CREATOR = new Parcelable.Creator<RecipeContent>() {
#Override
public RecipeContent createFromParcel(Parcel in) {
return new RecipeContent(in);
}
So, what I did and it helped was to arrange the in.read items in the same way as the values in the Parcel are arranged.
Here are the details:
public RecipeContent(int id, String recipeName, List<Ingredient> ingredients,
List<BakingStep> bakingSteps, String recipeImage) {
this.id = id;
this.recipeName = recipeName;
this.ingredients = ingredients;
this.bakingSteps = bakingSteps;
this.recipeImage = recipeImage;
}
And then using the same order for the in.read:
public RecipeContent(Parcel in) {
id = in.readInt();
recipeName = in.readString();
ingredients = in.readParcelable(Ingredient.class.getClassLoader());
bakingSteps = in.readParcelable(BakingStep.class.getClassLoader());
recipeImage = in.readString();
}
Hope this helps for you too.
Related
I'm trying to trasnfer data between activities with parcelable I'm having a bit of an issue. I'm getting the error Expression expected on the putExtraData portion of the code. The debugger says error: cannot find symbol variable ListingsModel. The class containing the parcelable code is called ListingsModel as is the constructor. I can’t seem to locate my error. My intent is wrapped in an if statement that should execute if try and launch the activity ReviewActivity.class and pass all data accordingly. Any help would be greatly appreciated.
Intent to launch Review activity class and pass data on true if statement:
Intent intent = new Intent(MainActivity.this, ReviewActivity.class);
putExtraData("ListingsModel", **ListingsModel**); //error appears here
startActivity(intent);
Class containing parcelable code:
public class ListingsModel implements Parcelable {
public static final int IMAGE_TYPE = 1;
public String title, street, city, state, zip, hours, isOpen, content, image, phone, timestamp;
public int type, rating, ratingCount;
public Double lat, lng;
public ListingsModel(int mtype, String mtitle, Integer mrating, Integer mRatingCount, String mstreet, String mcity, String mstate, String mzip, String mhours, String mIsOpen, String mcontent, String mimage, String mphone, Double mlat, Double mlng, String mtimestamp) {
this.type = mtype;
this.title = mtitle;
this.rating = mrating;
this.ratingCount = mRatingCount;
this.street = mstreet;
this.city = mcity;
this.state = mstate;
this.zip = mzip;
this.hours = mhours;
this.isOpen = mIsOpen;
this.content = mcontent;
this.image = mimage;
this.phone = mphone;
this.lat = mlat;
this.lng = mlng;
this.timestamp = mtimestamp;
}
//write object values to parcel for storage
public void writeToParcel(Parcel dest, int flags){
//write all properties to the parcle
//dest.writeInt(type);
dest.writeString(title);
dest.writeInt(rating);
dest.writeInt(ratingCount);
dest.writeString(street);
dest.writeString(city);
dest.writeString(state);
dest.writeString(zip);
dest.writeString(hours);
dest.writeString(isOpen);
dest.writeString(content);
dest.writeString(image);
dest.writeString(phone);
dest.writeDouble(lat);
dest.writeDouble(lng);
}
//constructor used for parcel
public ListingsModel(Parcel parcel){
//read and set saved values from parcel
title = parcel.readString();
rating = parcel.readInt();
ratingCount = parcel.readInt();
street = parcel.readString();
city = parcel.readString();
state = parcel.readString();
zip = parcel.readString();
hours = parcel.readString();
isOpen = parcel.readString();
content = parcel.readString();
image = parcel.readString();
phone = parcel.readString();
lat = parcel.readDouble();
lng = parcel.readDouble();
}
//creator - used when un-parceling our parcle (creating the object)
public static final Parcelable.Creator<ListingsModel> CREATOR = new Parcelable.Creator<ListingsModel>(){
#Override
public ListingsModel createFromParcel(Parcel parcel) {
return new ListingsModel(parcel);
}
#Override
public ListingsModel[] newArray(int size) {
return new ListingsModel[0];
}
};
//return hashcode of object
public int describeContents() {
return hashCode();
}
}
I need the activity ReviewActivity to lauch pre-poulated with the parcelable data. I can't figure out the issue that's causing the error on the putExtraData portion of my intent call.
When passing model as parcelable use this:
Bundle b = new Bundle();
b.putParcelable("keyName", YourModel);
startActivity(new Intent(this, Activity.class).putExtra("bundleName", bundle));
Receive model as parcelable:
Bundle bundle= getIntent().getBundleExtra("bundleName");
bundle.getParcelable("keyName");
ListingModel listingModel = new ListingModel(/* put required parameters here*/);
Intent intent = new Intent(this, ReviewActivity.class);
intent.putExtra("ListingsModel", listingModel);
startActivity(intent);
I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .
I suppose for a Map< String , String > below is the correct implementation:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,String> entry : map.entrySet()){
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
String value = in.readString();
map.put(key,value);
}
}
But what about Map < String , Object > ?
You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:
public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,Parcelable> entry : map.entrySet()){
out.writeString(entry.getKey());
entry.getValue().writeToParcel(out, flags);
}
}
private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
Parcelable value = in.readParcelable(getClass().getClassLoader());
map.put(key,value);
}
}
HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:
public void writeToParcel(Parcel out, int flags){
out.writeValue(map);
}
private MyParcelable(Parcel in){
map = (Map)in.readValue(getClass().getClassLoader());
}
See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.
NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.
I am using parcelable to pass class to new activity. However, at receiving side, I get empty values. It is strange that I get empty values and not null values. However, I do expect some values in each attribute. I have checked that the attributes are not empty at the caller part. Why is this so?
Here is my implementation:
Call new activity
Intent intent = new Intent(ProductDetailsActivity.this, ShopActivity.class);
intent.putExtra(ShopActivity.KEY_COMPANY, products.get(currentTypePosition).companyDetails);
startActivity(intent);
Receive
Company company = getIntent().getParcelableExtra(KEY_COMPANY);
Parcelable
public class Company implements Parcelable {
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Company createFromParcel(Parcel in) {
return new Company(in);
}
public Company[] newArray(int size) {
return new Company[size];
}
};
// TODO: Remove default values once backend is completed
public ArrayList<CompanyProduct> companyProducts = getDefaultCompanyProducts();
public String name = "hello";
public int icon = 0;
public int productCount = 100;
public int followerCount = 9000;
public String location = "hello";
public String contact = "400-23729";
public String description = "hello";
public int storeCount = 100;
public Company() {
super();
}
public Company(Parcel in) {
in.readTypedList(companyProducts, Status.CREATOR);
name = in.readString();
icon= in.readInt();
productCount = in.readInt();
followerCount = in.readInt();
location = in.readString();
contact = in.readString();
description = in.readString();
storeCount = in.readInt();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(companyProducts);
dest.writeString(name);
dest.writeInt(icon);
dest.writeInt(productCount);
dest.writeInt(followerCount);
dest.writeString(location);
dest.writeString(contact);
dest.writeString(description);
dest.writeInt(storeCount);
}
}
I had to pass a dataobject from one activity to another. The best way to do this is uing Parcelable.
The dataobject had some fields with setter and getter methods. After setting some fields and passing the object to another activity, wha tI observed is that the field values got interchanged to other field values.
The order of fields for writing to parcel and reading from parcel is the same.
public void writeToParcel(Parcel out, int flags) {
out.writeInt(id);
out.writeString(appNo);
out.writeString(this.policyNo);
out.writeInt((int)this.AppRcptDt.getTime());
out.writeString(this.currentStatus);
out.writeString(this.productCd);
out.writeDouble(this.sumAssured);
out.writeDouble(this.modalPremium);
out.writeDouble(this.annualPremium);
out.writeString(this.paymentMode);
out.writeString(this.branchCd);
out.writeString(this.branchName);
out.writeString(this.insuredName);
out.writeString(this.auraStatus);
out.writeString(this.ownerName);
out.writeString(this.agentCd);
out.writeString(this.billingMode);
}
private ApplicationTrackerDO(Parcel in) {
id=in.readInt();
this.appNo = in.readString();
this.policyNo = in.readString();
this.AppRcptDt = new Date(in.readLong());
this.currentStatus = in.readString();
this.productCd = in.readString();
this.sumAssured = in.readDouble();
this.modalPremium = in.readDouble();
this.annualPremium = in.readDouble();
this.paymentMode = in.readString();
this.branchCd = in.readString();
this.branchName = in.readString();
this.insuredName = in.readString();
this.auraStatus = in.readString();
this.ownerName = in.readString();
this.agentCd = in.readString();
this.billingMode = in.readString();
}
It is not the order but the data type that is not the same, from the first 4 lines you write int, string, string, int then you read int, string, string, long. I didn't check any further, you must match both order and datatype of read and write operations.
You are writing int
out.writeInt((int)this.AppRcptDt.getTime());
But reading long
this.AppRcptDt = new Date(in.readLong());
my solution to provide an date object with null
#Override
public void writeToParcel ( Parcel dest, int flags ) {
dest.writeInt((birthday != null) ? 1 : 0); // is birthday set?
dest.writeLong((birthday != null) ? birthday.getTime() : 0);
}
public void readFromParcel ( Parcel in ) {
if(in.readInt() == 1) {
birthday = new Date(in.readLong());
} else {
in.readLong(); // ignore stored value
birthday = null;
}
}
java.util.Date implements Serializable:
public class Date implements Serializable, Cloneable, Comparable<Date> {
private static final long serialVersionUID = 7523967970034938905L;
// Used by parse()
private static final int CREATION_YEAR = new Date().getYear();
private transient long milliseconds;
Use:
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(myDateField);
}
private MyClass(Parcel in) {
myDateField = (Date)in.readSerializable();
}
I have Product class.I want to pass product object one activity to another.
I have implemented like this :
public class Product implements Parcelable{
private double availableQuantity;
private double price;
private String productCode;
private String description;
private String nonStockItemFlag;
private String activeFlag;
private String kitProductFlag;
private double value;
private ArrayList<Product> product;
private double qty;
public Product() {
}
/**
* #param availableQuantity
* #param price
* #param productCode
* #param description
* #param nonStockItemFlag
* #param kitProductFlag
* #param qty
* #param grossValue
* #param value
*/
public Product(double availableQuantity, double price, String productCode,
String description, String nonStockItemFlag, String kitProductFlag,
double qty, double value) {
super();
this.availableQuantity = availableQuantity;
this.price = price;
this.productCode = productCode;
this.description = description;
this.nonStockItemFlag = nonStockItemFlag;
this.kitProductFlag = kitProductFlag;
this.qty = qty;
this.value = value;
}
// setter & getter
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
Bundle b = new Bundle();
b.putParcelableArrayList("enteredProduct", product);
dest.writeBundle(b);
}
public static final Parcelable.Creator<Product> CREATOR = new Parcelable.Creator<Product>() {
public Product createFromParcel(Parcel in) {
Product prod = new Product();
Bundle b = in.readBundle(Product.class.getClassLoader());
prod.product = b.getParcelableArrayList("enteredProduct");
System.out.println("***product***" + prod.product.get(0).getPrice());
return prod;
}
public Product[] newArray(int size) {
return new Product[size];
}
};
This is caller part :
if(productMap.size() >0){
ArrayList<Product> enteredProductList = new ArrayList<Product>(productMap.values());
System.out.println("-enteredProductList --" + enteredProductList.size());
System.out.println("--- " +enteredProductList.get(0).getPrice() );
Bundle b = new Bundle();
b.putParcelableArrayList("enteredProduct", enteredProductList);
Intent showContent = new Intent(getApplicationContext(),RetailerOrderIActivity.class);
showContent.putExtras(b); //Insert the Bundle object in the Intent' Extras
startActivity(showContent);
}else{
Toast.makeText(RetailerOrderActivity.this," You don't have invoice records" ,Toast.LENGTH_SHORT).show();
}
This is receive part :
Bundle b = this.getIntent().getExtras();
ArrayList<Product> p = b.getParcelableArrayList("enteredProduct");
System.out.println("-- RetailerOrderIActivity --" + p.size() );
for(Product s : p){
System.out.println(" --Qty-" + s.getQty());
System.out.println(" --price -" + s.getPrice());
System.out.println(" --code -" + s.getProductCode());
}
The receiving part return null value.But in the sending Activity part contain value.Please correct my code?
what is wrong in my code?
I have lot of property for product entity calss.But i want to set some of entity.
Thanks in advance.
I guess you misunderstood the Parcelable examples.
You have to put all needed elements into the parcel, and get it from it later:
To write your object to the parcel, this is needed:
public void writeToParcel(Parcel dest, int flags)
{
dest.writeString(productCodce);
dest.writeString(description);
// and all other elements
}
Plus, you need a constructor receiving a parcel:
public Product(Parcel in)
{
this.productCode=in.readString();
this.description=in.readString();
// and all other elements
}
Your Creator should be something like:
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
{
public Product createFromParcel(Parcel in) { return new Product(in); }
public Product[] newArray(int size) { return new Product[size]; }
};
Now, at your activity level (NOT in your Product class!):
Push it into extras, use:
bundle.putParcelableArrayList("whatevername",products);
To get it from the extras, use a simple:
ArrayList<Product> products=bundle.getParcelableArrayList("whatevername");