简体   繁体   中英

pass arraylist via android activity

  System.out.println("here "); Bundle extras = getIntent().getExtras(); System.out.println("here2 "); ArrayList<Question> ques = extras.getParcelableArrayList("authoritems"); System.out.println("here "+ques.size()); Intent i = new Intent(getApplicationContext(), ListNameActivity.class); System.out.println("accounitems "+accountItems.size()); Bundle b = new Bundle(); b.putParcelableArrayList("authoritems", accountItems); i.putExtras(b); startActivity(i); overridePendingTransition(R.anim.dux_qact_fade_in, R.anim.dux_qact_fade_out); finish(); 

I get printed accounitems 35 then i get printed here then printed here 2 but it crashes on ArrayList<Question> ques = extras.getParcelableArrayList("authoritems"); why is that? and what am i doing wrong?

The class question is below

  import android.os.Parcel; import android.os.Parcelable; public class Question implements Parcelable { public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } 
public String getSubTitle() {
    return subTitle;
}

public void setSubTitle(String subTitle) {
    this.subTitle = subTitle;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
}

String title="";
String subTitle="";
String id="";
String url="";
String latitude="";
String longitude="";

public Question(String title, String subTitle, String id, String url, String latitude, String longitude) {
    this.title = title;
    this.subTitle = subTitle;
    this.id = id;
    this.url = url;
    this.latitude = latitude;
    this.longitude = longitude;
}

// Your existing methods go here. (There is no need for me to re-write them.) 

// The following methods that are required for using Parcelable
private Question(Parcel in) {
    // This order must match the order in writeToParcel()
    title = in.readString();
    subTitle = in.readString();
    id = in.readString();
    url = in.readString();
    latitude = in.readString();
    longitude = in.readString();
    // Continue doing this for the rest of your member data
}

public void writeToParcel(Parcel out, int flags) {
    // Again this order must match the Question(Parcel) constructor
    out.writeString(title);
    out.writeString(subTitle);
    out.writeString(id);
    out.writeString(url);
    out.writeString(latitude);
    out.writeString(longitude);
    // Again continue doing this for the rest of your member data
}

// Just cut and paste this for now
public int describeContents() {
    return 0;
}

}

As you are getting the error Parcelable.Creator object called CREATOR on class com.netvariant.zain.model.Question you need to implement a creator in your Question class, something like this:

  public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
     public Question createFromParcel(Parcel in) {
           ///
        }

       public Question[] newArray(int size) {
          ///
      }
   };

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