android - How to access an element of an ArrayList inside Parcelable object? -


i've been running difficulties while trying implement parcelable number of objects. far have project class of string , integer variables , object called parcelableproject contains single project class. difficulty starts when tried creating parcelable class name of allprojects, class contains single array list made of parcelableproject objects.

i have method move projects through intent:

    public void addproject(parcelableproject p){          intent = new intent(this.getactivity(), projectsfragment.class);          arraylist<parcelableproject> data = new arraylist<parcelableproject>();          data.add(p);          i.putparcelablearraylistextra("projects", data);      } 

furthermore, in projectsfragment try access arraylist data via:

    intent = getactivity().getintent();     bundle data=i.getextras();     arraylist projects=  data.getparcelable("projects");     parcelableproject pproj=projects.get(0); 

but gives me "cannot convert object parcelableproject" error. proper way access project's variables? there going list of them passed projectsfragment parcelable needs contain arraylist of projects, don't know how access individual ones.

try this

public class employeedata implements parcelable {  string empname; string empid; string empaddress;  public employeedata(parcel in) {     readfromparcel(in); }  public employeedata(string name, string id, string address) {      this.empname = name;     this.empid = id;     this.empaddress = address; }  @override public int describecontents() {     // todo auto-generated method stub     return 0; }  private void readfromparcel(parcel in) {      empname = in.readstring();     empid = in.readstring();     empaddress = in.readstring();  }  @override public void writetoparcel(parcel dest, int flags) {     // todo auto-generated method stub     dest.writestring(empname);     dest.writestring(empid);     dest.writestring(empaddress);  }  public static final parcelable.creator<employeedata> creator = new creator<employeedata>() {      @override     public employeedata[] newarray(int size) {         // todo auto-generated method stub         return new employeedata[size];     }      @override     public employeedata createfromparcel(parcel source) {         // todo auto-generated method stub         return new employeedata(source);     } };  public string getempname() {     return empname; }  public void setempname(string empname) {     this.empname = empname; }  public string getempid() {     return empid; }  public void setempid(string empid) {     this.empid = empid; }  public string getempaddress() {     return empaddress; }  public void setempaddress(string empaddress) {     this.empaddress = empaddress; }  } 

put object like:

employeedata empdata = new employeedata(name, id, address);              bundle bundle = new bundle();             bundle.putparcelable("data", empdata);              intent intent = new intent(mainactivity.this, secondactivity.class);             intent.putextras(bundle); 

get object like:

employeedata empdata = getintent().getparcelableextra("data"); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -