简体   繁体   中英

Struggling to writing my object to Parcelable

I am trying to send an object from one activity to another, so hence I am using parcelable , and while I have created code to send it and receive it,(the code for this is at the bottom) it seems that I need some code to be able to actually write the object to the parcel.

Error While Passing An Object From An Activity To Another (Using Parcelable) I believe what I need to do is similar to the answer given in this, so I need a writeToParcel method, which I have done in the code below. (Although at dest.writeValue(this); is where I get an error) says StackOverFlowError

I believe i may also need public static final Parcelable.Creator ... although don't completely know how to write it (I have tried to write one roughly and its the bit in comments)

Also I don't know if I need a bit that would be like public Clubs (Parcel source) ...

Any help would be greatly appreciated. Thanks

public class Clubs implements Parcelable{   
        public void setEvent(String eventType, String date) {
            this.eventType = eventType;
            this.date = date; 
        }

   //contains lots of defined variables and various methods that 
   //aren't relevant for my question and would take up lots of room
   //all like the one above.


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

        @Override
        public int describeContents() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
                  dest.writeValue(this);
        }

}

My onItemClick class that puts the object into the parcel, and starts the new activity

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                Clubs mymeeting = db.get(map.get(position));
                Intent i = new Intent();
                Bundle b = new Bundle();
                b.putParcelable("mymeeting", mymeeting);
                i.putExtras(b);
                i.setClass(ListSample.this, DynamicEvents.class);
                startActivity(i);
            }

The start of my new activity code that will be edit later once it send the object across correctly

public class DynamicEvents extends Activity
{
  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
        Bundle b = getIntent().getExtras();
        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(20);
        textView.setText(" " + b.getParcelable("mymeeting").toString());

        // Set the text view as the activity layout
        setContentView(textView);
  } 
}  

Rewrite writeToParcel method as

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(eventType);
    dest.writeString(date);
}

You may not be allowed to write Custom Java Object directly. Either write standard data values individually or make your object serializable and use method writeSerializable on Parcel object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM