AlexioHill
AlexioHill

Reputation: 49

Cannot access my Parcelable objects methods

I have passed my object using Parcelable to the new activity, and also written into the writeToParcel. I believe that the object has been transferred, as i can use .toString() however it has none of its associated methods. i used this link: http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/ my code runs, however I was expecting to be able to use a.clubName() or some such to be able to access these details, either i'm trying to access the details incorrectly or I have not quite got the set up correct.

Thanks for any help.

This is my Clubs class that associates the details to the parcel

public class Clubs implements Parcelable{   
        //lots of variables defining things such as clubName, address etc.

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator(){
                public Clubs createFromParcel(Parcel in) {
                    return new Clubs(in); }  
                public Clubs[] newArray(int size) {
                    return new Clubs[size]; } 
                };  

        private void readFromParcel(Parcel in) {  
            clubName = in.readString(); 
            address = in.readString(); 
            postcode = in.readString(); 
            contactName = in.readString(); 
            contactPhone = in.readString(); 
            date = in.readString(); 
            eventType = in.readString(); 
            scrutTime = in.readString(); 
            startTime = in.readString(); 
            eventName = in.readString(); 
            week = in.readString(); 
        }
    @Override   
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(clubName); 
        dest.writeString(address);
        dest.writeString(postcode);
        dest.writeString(contactName);
        dest.writeString(contactPhone);
        dest.writeString(date);
        dest.writeString(eventType);
        dest.writeString(scrutTime);
        dest.writeString(startTime);
        dest.writeString(eventName);
        dest.writeString(week);
    }

    public Clubs(Parcel in) { readFromParcel(in); }

This is my onItemClick method that starts the new activity that should send the object across

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

        });

this is the class i want to be able to write the details into

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);
        Object a = b.getParcelable("mymeeting");
        textView.setText(a.toString());

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

Upvotes: 0

Views: 1181

Answers (1)

Exoit
Exoit

Reputation: 76

You need to cast the parcelable to the correct class type.

Parcelable parcelable = b.getParcelable("mymeeting");
if(parcelable instanceof Clubs) {
   Clubs clubs = (Clubs) parcelable;
}

Upvotes: 1

Related Questions