java - Android passing an arraylist back to parent activity -


i've been searching simple example of no luck.

in android application have 2 activities: 1. main activity launched @ startup 2. second activity launched pressing button on main activty.

when second activity finished (by pressing button) want send arraylist of type myobject main activity , close itself, main activity can whatever it. how go achieving this? have been trying few things crashing application when start second activity.

when user presses button launch second activity:

            intent = new intent(mainactivity.this, secondactivity.class);             startactivityforresult(i, 1); 

the array bundled after pressing button on second activity:

intent intent= getintent();                     bundle b = new bundle();                     b.putparcelablearraylist("myarraylist", mylist);                     intent.putextras(b);                     setresult(result_ok, intent);                     finish(); 

and listener on main activity (although i'm not sure of 100% when code launches...)

    protected void onactivityresult(int requestcode, int resultcode, intent data) {           super.onactivityresult(requestcode, resultcode, data);           if(resultcode==result_ok && requestcode==1){         bundle extras = data.getextras();          final arraylist<myobject> mylist = extras.getparcelablearraylist("myarraylist");         toast.maketext(mainactivity.this, mylist.get(0).getname(), toast.length_short).show();     } } 

any ideas going wrong? onactivityresult() seems crashing application.

edit: class parcelable methods:

import android.os.parcel; import android.os.parcelable;  public class plan implements parcelable{     private string name;     private string id;      public plan(){     }      public plan createfromparcel(parcel in) {         plan plan = new plan();         plan.setid(in.readstring());         plan.setname(in.readstring());         return plan; }      public plan(string name, string id){         this.name = name;         this.id = id;     }       public string getname(){         return name;     }     public void setname(string name){         this.name = name;     }     public string getid(){         return id;     }     public void setid(string id){         this.id = id;     }      public string tostring(){         return "plan id: " + id + " plan name: " + name;     }      @override     public int describecontents() {         // todo auto-generated method stub         return 0;     }      @override     public void writetoparcel(parcel dest, int flags) {         dest.writestring(id);         dest.writestring(name);     }      public static final parcelable.creator<plan> creator     = new parcelable.creator<plan>() {         public plan createfromparcel(parcel in) {             return new plan();         }          @override         public plan[] newarray(int size) {             // todo auto-generated method stub             return new plan[size];         }      };  } 

after second activity finished, onactivityresult called, nothing displays inside toast, blank. ideas? i'm guessing class still messed up...

edit: got work

i had method peter supplied in wrong place. should inside creator, this:

public static final parcelable.creator<plan> creator = new parcelable.creator<plan>() {     public plan createfromparcel(parcel in) {         plan plan = new plan();         plan.setid(in.readstring());         plan.setname(in.readstring());         return plan; } 

and not out on own.

many peter! hope helps else.

your class myobject must implement parcelable in order serialized/deserialized android when put inside bundle.

update:

the method name createfromparcel. , have create object plan data in parcel:

public plan createfromparcel(parcel in) {         plan plan = new plan();         plan.setid(in.readstring());         plan.setname(in.readstring());         return plan; } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -