【问题标题】:sending a customized object to intent android向intent android发送自定义对象
【发布时间】:2015-10-05 06:46:14
【问题描述】:

我想将 Annonce 类型的对象传递给 Intent。如您所见,如果类属性是基元,这很简单,但是;就我而言,我有一个图像(位图)和一个Client 类型的属性(我创建了一个Client 类)。 我的解决方案是访问Client属性(使用getter和setter)并在writeToParcel方法中一一解析(需要太多时间),对于图像,我将其发送到mainActivity使用ByteArrayOutputStream。任何人都可以在Annonce 课堂上帮我完成这一切。

public class Annonce implements Parcelable {

    String article, desc, temps, ville, categorie;
    int prix;
    Bitmap img;
    Client c;

    public Annonce(String article, String desc, String temps, String ville,
            String categorie, int prix, Bitmap img, Client c) {
        this.article = article;
        this.desc = desc;
        this.c = c;
        this.prix = prix;
        this.img = img;
        this.temps = temps;
        this.categorie = categorie;
        this.ville = ville;
    }

    public static final Parcelable.Creator<Annonce> CREATOR = new Parcelable.Creator<Annonce>() {

        public Annonce createFromParcel(Parcel source) {
            return new Annonce(source);
        }

        public Annonce[] newArray(int size) {
            return new Annonce[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(article);
        parcel.writeString(desc);
        parcel.writeString(temps);
        parcel.writeString(ville);
        parcel.writeString(categorie);
        parcel.writeInt(prix);
    }

    public Annonce(Parcel source) {
        article = source.readString();
        desc = source.readString();
        temps = source.readString();
        ville = source.readString();
        categorie = source.readString();
        prix = source.readInt();
    }
}

【问题讨论】:

  • Serializable 方法怎么样?
  • 有没有parcelable的解决方案?
  • 在 parcelable 中,您可以将 Client 转换为 Object,反之亦然。但是位图永远不能被传递..在去位图之前应该是一个字节[]
  • 以防万一,这些位图通常有多大?
  • 不要在Annotate 中进行操作,在AsyncTask 中进行磁盘操作。也请把扩展讨论移到聊天中。这不是 Stack Overflow 上的 cmets 的用途。

标签: android android-intent parcelable


【解决方案1】:

具有“位图”类型的属性不是一个好的解决方案。取而代之的是,我们可以使用图像的路径来引用位图图像。 另外,我们可以将 Client 转换为 Parcelable 中的对象,以便通过 Intent 发送。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2014-11-14
    • 1970-01-01
    相关资源
    最近更新 更多