shaon007
shaon007

Reputation: 163

How to use Parcelable for a class which has multiple constructors?

Well, i was trying to pass arraylist of objects from one activity to another. I have 2 constructors in the class Student. If, i use, Serializable than the code is like below:

@SuppressWarnings("serial")
public class Student implements Serializable
{
private int studentdID;
private String studentName;
private String studentDept;

  public Student(){}

  public Student(String name, String dpt)
{ this.studentName = name;
  this.studentDept = dpt;}

  public Student(int id, String name, String dpt)
{ this.studentdID = id;
  this.studentName = name;
  this.studentDept = dpt; }

  public int getstudentdID() { return studentdID;   }
  public void setstudentdID(int studentdID) {this.studentdID = studentdID;}

  public String getstudentName() { return studentName;}
  public void setstudentName(String studentName) {this.studentName = studentName;}

  public String getstudentDept() {  return studentDept; }
  public void setstudentDept(String studentDept) { this.studentDept = studentDept;}
}

But the problem i am facing is that how am i going to do this with parcelable? How am i going to set the values of the variables in class-like i did with Serializable? I mean separately using 2 constructors-one without ID another without the ID?

Upvotes: 1

Views: 3254

Answers (3)

RKB
RKB

Reputation: 81

@u3l solution is not required..how many constructors are there it doesn't matter. simple it works go as normal implementation. I mean no special care is required when multiple constructors present in parcelable.

Upvotes: 0

u3l
u3l

Reputation: 3412

Marco's answer explains why Parcelable doesn't automatically decide what constructor to use - it can't.

However, there is a way around this. Use Parcel.dataAvail(), which

Returns the amount of data remaining to be read from the parcel. That is, dataSize()-dataPosition().

For example,

  public Student(){}

  public Student(String name, String dpt)
  { 
    this.studentName = name;
    this.studentDept = dpt;}

  public Student(int id, String name, String dpt)
  { this.studentdID = id;
    this.studentName = name;
    this.studentDept = dpt; 
  }

  public Student(Parcel in) {

     name = in.readString();
     dpt = in.readString();

     if(in.dataAvail() > 0) // is there data left to read?
          id = in.readInt();
  }

^ The above constructor will allow for the necessary variables to be instantiated correctly. Also, you define writeToParcel() something like:

public void writeToParcel(Parcel out) {
    out.writeString(name);
    out.writeString(dpt);

    //0 is the default value of id if you didn't initialize it like 
   // in the first constructor. If it isn't 0, that means it was initialized.
    if(id != 0)
         out.writeInt(id);
}

Of course, you'll need to define your CREATOR like so:

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

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

};

Upvotes: 3

Marco Acierno
Marco Acierno

Reputation: 14847

Did you read how Parcelable works?

You need only one constrcutor for parcelable to read what you pass to it, and Parcelable interface will add a method writeToParcel where you put the data to save.

It's not an automatic process like Serializable, everything is up to you.

The constructor which Parcelable will use will accept only one argument Parcel where you will find some methods like read*(KEY) to read back values.

And in writeToParcel you will write in the Parcel (the argument of the method) the values you want pass to pass with write*(KEY, VALUE).

Parcelable don't care about your constructors or fields.

P.S You will need a CREATOR too. Read some tutorial online to know more about it if you need.

Upvotes: 5

Related Questions