2

I wanted to pass the object from one class to other and my code is

    private MemberLoginResponse loginResponse;
    .
    .
    Intent intent = new Intent();
    intent = new Intent(context, BillingDeskActivity.class);
    intent.putExtra("privilages", loginResponse);
    startActivity(intent);

In other activity to receive the data I use this code

 Intent i_privilages = getIntent();
 privilages = (MemberLoginResponse) getIntent().getSerializableExtra("privilages");

and even I kept my class implements Serializable

My class where I struck ed in declaring arraylist

public class MemberLoginResponse implements Parcelable {
public Integer merchantMemberId = 0;
public Integer merchantId = 0;
public String merchantName = "";
public String merchantLogo = "";
public String merchantType = "";
public String merchantHQAddress = "";
public List<CustomSystemPrivilege> merchantPrivileges;
public List<CustomSystemPrivilege> privileges;
public StatusCode statusCode;
public Integer branchId = 0;

public MemberLoginResponse(Parcel in) {
    merchantMemberId = in.readInt();
    merchantId = in.readInt();
    merchantName = in.readString();
    merchantLogo = in.readString();
    merchantType = in.readString();
    merchantHQAddress = in.readString();
    in.readList(merchantPrivileges,
            CustomSystemPrivilege.class.getClassLoader());
    in.readList(privileges, CustomSystemPrivilege.class.getClassLoader());
    branchId = in.readInt();
}

public MemberLoginResponse() {
    // TODO Auto-generated constructor stub
}

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

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

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(merchantMemberId);
    dest.writeInt(merchantId);
    dest.writeString(merchantName);
    dest.writeString(merchantLogo);
    dest.writeString(merchantType);
    dest.writeString(merchantHQAddress);
    dest.writeList(merchantPrivileges);
    dest.writeList(privileges);
    dest.writeInt(branchId);
}

} }

sarabu
  • 503
  • 1
  • 8
  • 20

3 Answers3

2

Following is the code to read/ write another parcelable object into parcelable.

class MemberLoginResponse implements Parcelable {
//    
private StatusCode statusCode;
public void writeToParcel(Parcel dest, int flags) {

        dest.writeParcelable(statusCode , flags);

    }
}

if you want to read it use this

 statusCode = (StatusCode)in.readParcelable(A.class.getClassLoader());
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • Still cant figure it out, although I have done everything its not showing the values and shows null pointer exception in another activity. – sarabu Jul 09 '13 at 09:54
  • Share the code where you write. Did you initialized statusCode? – Pankaj Kumar Jul 09 '13 at 09:56
  • Now this line shows error ` in.readList(merchantPrivileges,CustomSystemPrivilege.class.getClassLoader());` i.e. in MemberLoginResponse – sarabu Jul 09 '13 at 10:13
  • 1
    You are doing wrong here. you should initialise the list. as `merchantPrivileges = new ArrayList();' before read. – Pankaj Kumar Jul 09 '13 at 10:23
0

try it in following way

public class clsChart implements Parcelable{
private int chartID;
private String chartName;
private int chartType;
public clsChart(){

}

@SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR =
        new Parcelable.Creator() {
            @Override
            public clsChart createFromParcel(Parcel in) {
                return new clsChart(in);
            }

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

    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(chartID);
    dest.writeString(chartName);
    dest.writeInt(chartType);

}
private void readFromParcel(Parcel in) {
    chartID = in.readInt();
    chartName = in.readString();
    chartType = in.readInt();

}

 public int getChartID() {
    return chartID;
}
public void setChartID(int chartID) {
    this.chartID = chartID;
}
public String getChartName() {
    return chartName;
}
public void setChartName(String chartName) {
    this.chartName = chartName;
}
public int getChartType() {
    return chartType;
}
public void setChartType(int chartType) {
    this.chartType = chartType;
}
public clsChart(Parcel in) {
     readFromParcel(in);
}
public clsChart( int chartID,String chartName,int chartType)
{
    this.chartID = chartID;
    this.chartName = chartName;
    this.chartType = chartType;
}

}

now Bundle b=new Bundle();
b.putParcelable("your_key",your_object_which_implements_parcelable(here chart object));
intent.putExtras(b);
Software Sainath
  • 1,040
  • 2
  • 14
  • 39
  • I have a class 'StatusCode statusCode;'. How to readFromParcel and writeToParcel. In that StatusCode class again I have set of variables – sarabu Jul 09 '13 at 07:26
  • Follow the same what i did for clsChart Class there is nothing confusion on it right – Software Sainath Jul 09 '13 at 07:44
  • clsChart has the variables that contains int,String.. But if you check my class that contatins `StatusCode`. class i.e. object so how to declare that one in readFromParcel and writeToParcel – sarabu Jul 09 '13 at 07:48
  • try in.readParcelable() and in.writeParcelable() your StatusCode should implement parcelable – Software Sainath Jul 09 '13 at 07:52
  • If i use bundle it force closes and shows runtimeexception but if I use Intent it takes null values – sarabu Jul 09 '13 at 09:56
0

If you're just passing objects around then Parcelable was designed for this. It requires a little more effort to use than using Java's native serialization, but it's way faster.

From the docs, a simple example for how to implement is:

// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
    private int mData;

    /* everything below here is for implementing Parcelable */

    // 99.9% of the time you can just ignore this
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);
        }

        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];
        }
    };

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private MyParcelable(Parcel in) {
        mData = in.readInt();
    }
}

Observe that in the case you have more than one field to retrieve from a given Parcel, you must do this in the same order you put them in (that is, in a FIFO approach).

Once you have your objects implement Parcelable it's just a matter of putting them into your Intents with putExtra():

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

Then you can pull them back out with getParcelableExtra():

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");

Hope this will guide & help you. :)

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • In my code I have different classes I was unable to read and write for StatusCode and CustomSystemPrivilege like mData in your class. – sarabu Jul 09 '13 at 10:19