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");
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 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'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.
I want to pass an object of type Annonce to an Intent. As you can see, it's simple if the class attributes are primitives, However; in my case I have an image (Bitmap) and an attribute of type Client ( I have created a Client class).
My solution is to access the Client attributes (using getter and setter) and parsing it in the writeToParcel method one by one (it takes too much time), and for the image, I am sending it in the mainActivity using ByteArrayOutputStream. Can anyone help me do it all in Annonce class.
public class Annonce implements Parcelable {
String article, desc, temps, ville, categorie;
int prix;
Bitmap img;
Client c;
public Annonce(String article, String desc, String temps, String ville,
String categorie, int prix, Bitmap img, Client c) {
this.article = article;
this.desc = desc;
this.c = c;
this.prix = prix;
this.img = img;
this.temps = temps;
this.categorie = categorie;
this.ville = ville;
}
public static final Parcelable.Creator<Annonce> CREATOR = new Parcelable.Creator<Annonce>() {
public Annonce createFromParcel(Parcel source) {
return new Annonce(source);
}
public Annonce[] newArray(int size) {
return new Annonce[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(article);
parcel.writeString(desc);
parcel.writeString(temps);
parcel.writeString(ville);
parcel.writeString(categorie);
parcel.writeInt(prix);
}
public Annonce(Parcel source) {
article = source.readString();
desc = source.readString();
temps = source.readString();
ville = source.readString();
categorie = source.readString();
prix = source.readInt();
}
}
Having an attribut of type "bitmap" is not a good solution . Instead of that , we can use the path of the image to refer to the bitmap image .
Also, we can convert the Client into object in parcelable in order to send it through intent.
This is my JSON string:
[{"BranchID":1,"SecurityCode1":13,"SecurityCode2":14,"PrintHeight":10,"PrintWidth":10,"Active":true}]
This is Code I am using to parse the JSON:
Type t = new TypeToken<List<Setting>>() {
}.getClass();
String json = ServiceHelper.getJSON(response);
List<Setting> list = (List<Setting>) gson.fromJson(json, t);
//list is null which it souldnt
This is the Setting class, Entity is ORMDroid entity class:
public class Setting extends Entity {
#Column(name = "id", primaryKey = true)
#SerializedName("BranchID")
public int BranchID;
public int securityCodeLPK;
public int securityCodeSDK;
#SerializedName("PrintHeight")
public int PrintHeight;
#SerializedName("PrintWidth")
public int PrintWidth;
#SerializedName("Active")
public String Active;
#SerializedName("SecurityCode1")
public String SecurityCode1;
#SerializedName("SecurityCode2")
public String SecurityCode2;
public Setting(int BranchID, int height, int width, String active, String securityCode1, String securityCode2) {
super();
BranchID = BranchID;
PrintHeight = height;
PrintWidth = width;
Active = active;
SecurityCode1 = securityCode1;
SecurityCode2 = securityCode2;
}
public Setting () {
super();
}
}
It seems to be OK, but list after gson.FromJson is null. What's wrong with this code?
Please help
You should use getType() instead of getClass().
Type t = new TypeToken<List<Setting>>() {}.getType();