Transfer Data between Activities with Android Parcelable - android

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);

Related

ByteArray Property Too Large to send within putExtra("obj", obj) of intent when opening another Activity

I have an Object that has string, int, double, byte[], properties. My byte[] is storing an image from the database as i store the images in byte inside my tables.
The problem is that when i serialize the object and pass the object through intent.puteExtra("obj", obj) the app crashes without any errors in the console.
I then made my byte[] property = byte[1] and all worked the activity swithed and the data passes through the intent.
Here is my Sell Obj:
import android.media.Image;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
import java.util.Date;
public class Sell extends Product implements Serializable
{
public byte[] Image1;
public byte[] Image1a;
public byte[] Image2;
public byte[] Image3;
public byte[] Image4;
public String postcode;
public double postage;
public int isDelivered ;
public Sell(int p_Id, String p_u_id, double price, String title, String description, int category, String tags,
byte[] image1, byte[] image2, byte[] image3, byte[] image4
, String postcode, double postage, int isDelivered, String date, int type)
{
super(p_Id, p_u_id,price,title,description,category,tags, type, date);
this.Image1 = image1;
this.Image2 = image2;
this.Image3 = image3;
this.Image4 = image4;
this.postcode = postcode;
this.postage = postage;
this.isDelivered = isDelivered;
}
}
Here is my Product Obj:
package com.asianretail.trikam.asianretail;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
public class Product implements Serializable {
public int p_Id;
public String P_U_Id;
public double Price;
public int Category;
public String Tags;
public String Title;
public String Description;
public int Type;
public String Date;
public Product(int p_Id, String p_u_id, double price, String title, String description, int category, String tags, int type, String date)
{
this.Date = date;
this.p_Id = p_Id;
this.P_U_Id = p_u_id;
this.Price = price;
this.Title = title;
this.Description = description;
this.Category = category;
this.Tags = tags;
this.Type = type;
}
}
To start the activity from the fragment i use this:
Product prod = null;
for(int i = 0; i < pObj.size(); i++){
if(((Product)pObj.get(i)).p_Id == ((int)v.getTag())){
prod = (Product)pObj.get(i);
}
}
Bundle bundle = new Bundle();
bundle.putSerializable("valueProd", prod);
Intent i = new Intent(getActivity(), ProductActivity.class);
i.putExtra("bundleProd", bundle);
startActivity(i);
Its better to write byte array to a file and pass the url of the file to bundle instead. You should not pass the image in bundle as it may try to access more memory than allocated in case your byte array too large.

Android Parcelable object return empty values

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);
}
}

sending a customized object to intent android

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.

Android Parcelable Issue

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();
}

Android Parcelable object return Null

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");

Categories

Resources