通过android活动传递arraylist

时间:2014-04-21 08:43:53

标签: android arraylist

        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();

我得到印刷的会计35 然后我在这里打印 然后在这里打印2 但它在ArrayList<Question> ques = extras.getParcelableArrayList("authoritems");上崩溃了 这是为什么?我做错了什么?

课堂问题在下面

  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;
}

}

1 个答案:

答案 0 :(得分:0)

当你收到错误Parcelable.Creator object called CREATOR on class com.netvariant.zain.model.Question时,你需要在Question课程中实现一个创建者,如下所示:

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

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