Android:getParcelable 返回 null?

发布于 2024-10-31 13:36:23 字数 3149 浏览 1 评论 0原文

作为我上一个问题的后续: 在活动之间传递 Arraylist?使用 Parcelable

我现在尝试通过 Parcelable 传递我的 ArrayList。但是,当从其他活动获取它时,它会返回 null。我似乎不明白为什么。我有以下内容...

ResultsList.java

 public class ResultsList extends ArrayList<SearchList> implements Parcelable {

    /**
 * 
 */
private static final long serialVersionUID = -78190146280643L;

 public ResultsList(){

}

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

@SuppressWarnings({ "rawtypes" })
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public ResultsList createFromParcel(Parcel in) {
        return new ResultsList(in);
    }

    public Object[] newArray(int arg0) {
        return null;
    }

};

private void readFromParcel(Parcel in) {
    this.clear();

    //First we have to read the list size
    int size = in.readInt();

    //Reading remember that we wrote first the Name and later the Phone Number.
    //Order is fundamental

    for (int i = 0; i < size; i++) {
        String title = in.readString();
        String Des = in.readString();
        String message = in.readString();
        SearchList c = new SearchList(title,Des,link);
        this.add(c);
    }

}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    int size = this.size();
    //We have to write the list size, we need him recreating the list
    dest.writeInt(size);
    //We decided arbitrarily to write first the Name and later the Phone Number.
    for (int i = 0; i < size; i++) {
        SearchList c = this.get(i);
        dest.writeString(c.gettitle());
        dest.writeString(c.getDescription());
        dest.writeString(c.getmessage());
    }
}
 }

SearchList.java

 public class SearchList {
private String title;
  private String description;
  private String message;
  public SearchList(String name, String phone, String mail) {
          super();
          this.title = name;
          this.description = phone;
          this.message = mail;
  }
  public String gettitle() {
          return title;
  }
  public void setTitle(String title) {
          this.title = title;
  }
  public String getDescription() {
          return description;
  }
  public void setDescription(String description) {
          this.description = description;
  }
  public String getmessage() {
          return message;
  }
  public void setmessage(String link) {
          this.message = message;
  }
 }

listOfResults 声明为 ResultsList listOfResults = new ResultsList(); 并在代码中前面填充然后发送与...

Intent intent = new Intent(this ,newthing.class);
    Bundle b = new Bundle();
    b.putParcelable("results", listOfResults);
    startActivityForResult(intent,0);

接收...

    Bundle extras = getIntent().getExtras();
    ResultsList results = extras.getParcelable("results");

我想指出我正在通过检查进入 Bundle 的 Arraylist 的 size() 来测试这个 null 东西,然后它出来。它进去很好,出来时为空。

As a follow on from my last question: Passing Arraylist between activities? Using Parcelable

I'm now attempting to pass my ArrayList via Parcelable. However, when it's gotten from the other activity it returns null. I can't seem to figure out why. I've got the following...

ResultsList.java

 public class ResultsList extends ArrayList<SearchList> implements Parcelable {

    /**
 * 
 */
private static final long serialVersionUID = -78190146280643L;

 public ResultsList(){

}

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

@SuppressWarnings({ "rawtypes" })
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public ResultsList createFromParcel(Parcel in) {
        return new ResultsList(in);
    }

    public Object[] newArray(int arg0) {
        return null;
    }

};

private void readFromParcel(Parcel in) {
    this.clear();

    //First we have to read the list size
    int size = in.readInt();

    //Reading remember that we wrote first the Name and later the Phone Number.
    //Order is fundamental

    for (int i = 0; i < size; i++) {
        String title = in.readString();
        String Des = in.readString();
        String message = in.readString();
        SearchList c = new SearchList(title,Des,link);
        this.add(c);
    }

}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    int size = this.size();
    //We have to write the list size, we need him recreating the list
    dest.writeInt(size);
    //We decided arbitrarily to write first the Name and later the Phone Number.
    for (int i = 0; i < size; i++) {
        SearchList c = this.get(i);
        dest.writeString(c.gettitle());
        dest.writeString(c.getDescription());
        dest.writeString(c.getmessage());
    }
}
 }

SearchList.java

 public class SearchList {
private String title;
  private String description;
  private String message;
  public SearchList(String name, String phone, String mail) {
          super();
          this.title = name;
          this.description = phone;
          this.message = mail;
  }
  public String gettitle() {
          return title;
  }
  public void setTitle(String title) {
          this.title = title;
  }
  public String getDescription() {
          return description;
  }
  public void setDescription(String description) {
          this.description = description;
  }
  public String getmessage() {
          return message;
  }
  public void setmessage(String link) {
          this.message = message;
  }
 }

listOfResults is declared as ResultsList listOfResults = new ResultsList(); and filled earlier in the code It's then sent with...

Intent intent = new Intent(this ,newthing.class);
    Bundle b = new Bundle();
    b.putParcelable("results", listOfResults);
    startActivityForResult(intent,0);

Receiving...

    Bundle extras = getIntent().getExtras();
    ResultsList results = extras.getParcelable("results");

I'd like to point out I'm testing this null thing by checking the size() of the Arraylist going into the Bundle and then it coming out. It goes in fine and comes out null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2024-11-07 13:36:23

没有将捆绑包附加到意图! (doh!) IRC 上一位好心的绅士指出了! #android-dev

Didn't attach bundle to intent! (doh!) was pointed out by a kind gent on IRC! #android-dev

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文